home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / g++-dist / ld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-09  |  127.6 KB  |  4,758 lines

  1. /* Linker `ld' for GNU
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman with some help from Eric Albert.
  19.    Set, indirect, and warning symbol features added by Randy Smith.  */   
  20.    
  21. /* Define how to initialize system-dependent header fields.  */
  22. #ifdef sun
  23. /* Use Sun's TARGET convention.  */
  24.   #ifndef TARGET
  25.     #define SUN2 2
  26.     #define SUN3 3
  27.     #define SUN4 4
  28.     #if defined(sparc)
  29.       #define TARGET SUN4
  30.     #else
  31.       #if defined(mc68020) || defined(m68020)
  32.         #define TARGET SUN3
  33.       #else
  34.         #define TARGET SUN2
  35.       #endif
  36.     #endif
  37.   #else
  38.     #define _CROSS_TARGET_ARCH TARGET  /* locate the correct a.out.h file */
  39.   #endif
  40. #endif
  41.  
  42. #include <ar.h>
  43. #include <stdio.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <sys/file.h>
  47. #include <sys/time.h>
  48. #include <sys/resource.h>
  49. #ifndef sony_news
  50. #include <fcntl.h>
  51. #endif
  52.  
  53. #ifdef COFF_ENCAPSULATE
  54. #include "a.out.encap.h"
  55. #else
  56. #include <a.out.h>
  57. #endif
  58.  
  59. #ifndef N_SET_MAGIC
  60. #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  61. #endif
  62.  
  63. /* If compiled with GNU C, use the built-in alloca */
  64. #ifdef __GNUC__
  65. #define alloca __builtin_alloca
  66. #endif
  67.  
  68. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  69.  
  70. #include "stab.h"
  71. #define CORE_ADDR unsigned long    /* For symseg.h */
  72. #include "symseg.h"
  73.  
  74. #ifdef USG
  75. #include <string.h>
  76. #else
  77. #include <strings.h>
  78. #endif
  79.  
  80. /* Determine whether we should attempt to handle (minimally)
  81.    N_BINCL and N_EINCL.  */
  82.  
  83. #if defined (__GNU_STAB__) || defined (N_BINCL)
  84. #define HAVE_SUN_STABS
  85. #endif
  86.  
  87. #define min(a,b) ((a) < (b) ? (a) : (b))
  88.  
  89. /* Macro to control the number of undefined references printed */
  90. #define MAX_UREFS_PRINTED    10
  91.  
  92. /* Size of a page; obtained from the operating system.  */
  93.  
  94. int page_size;
  95.  
  96. /* Name this program was invoked by.  */
  97.  
  98. char *progname;
  99.  
  100. /* System dependencies */
  101.  
  102. /* Define this if names etext, edata and end should not start with `_'.  */
  103. /* #define nounderscore 1 */
  104.  
  105. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  106.    whose native format is different.  */
  107. /* #define NON_NATIVE */
  108.  
  109. /* Define this to specify the default executable format.  */
  110.  
  111. #ifdef hpux
  112. #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  113. #endif
  114.  
  115. #ifndef DEFAULT_MAGIC
  116. #define DEFAULT_MAGIC ZMAGIC
  117. #endif
  118.  
  119. /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  120.  
  121. #ifndef N_TXTADDR
  122.   #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  123.     #define N_TXTADDR(X) 0
  124.   #endif
  125.   #ifdef is68k
  126.     #define N_TXTADDR(x)  (sizeof (struct exec))
  127.   #endif
  128.   #ifdef sequent
  129.     #define    N_TXTADDR(x) (N_ADDRADJ(x))
  130.   #endif
  131. #endif
  132.  
  133. #ifndef N_DATADDR
  134.   #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  135.     #define N_DATADDR(x) \
  136.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  137.     : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  138.   #endif
  139.   #ifdef is68k
  140.     #define SEGMENT_SIZE 0x20000
  141.     #define N_DATADDR(x) \
  142.     (((x).a_magic==Omagic)? (N_TXTADDR(x)+(x).a_text) \
  143.      : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  144.   #endif
  145.   #ifdef sequent
  146.     #define N_DATADDR(x) \
  147.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  148.     : (page_size+(((x).a_text-1) & ~(page_size-1))))
  149.   #endif
  150. #endif
  151.  
  152. #ifdef sun
  153.   #if TARGET == SUN4
  154.     #define INITIALIZE_HEADER \
  155.     {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  156.   #endif
  157.   #if TARGET == SUN2
  158.     #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  159.   #endif
  160.   #ifndef INITIALIZE_HEADER
  161.     #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  162.   #endif
  163. #endif
  164. #ifdef ALTOS
  165.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  166. #endif
  167. #ifdef is68k
  168.   #ifdef M_68020
  169.     /* ISI rel 4.0D doesn't use it, and rel 3.05 doesn't have an
  170.        a_machtype field and so won't recognize the magic number.  To keep
  171.        binary compatibility for now, just ignore it */
  172.     #define INITIALIZE_HEADER outheader.a_machtype = 0;
  173.   #endif
  174. #endif
  175. #ifdef hpux
  176.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, HP9000S200_ID)
  177. #endif
  178. #if defined(i386) && !defined(sequent)
  179.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  180. #endif
  181.  
  182. #ifdef is68k
  183.   /* This enables code to take care of an ugly hack in the ISI OS.
  184.      If a symbol beings with _$, then the object file is included only
  185.      if the rest of the symbol name has been referenced. */
  186.   #define DOLLAR_KLUDGE
  187. #endif
  188.  
  189. /*
  190.  * Alloca include.
  191.  */
  192. #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  193.   #include "alloca.h"
  194. #endif
  195.  
  196. #ifndef L_SET
  197.   #define L_SET 0
  198. #endif
  199.  
  200. /*
  201.  * Ok.  Following are the relocation information macros.  If your
  202.  * system should not be able to use the default set (below), you must
  203.  * define the following:
  204.  
  205.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  206.  * of structure that is stored in the relocation info section of your
  207.  * a.out files.  Often this is defined in the a.out.h for your system.
  208.  *
  209.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  210.  * <whatever> to be relocated.  *Must be an lvalue*.
  211.  *
  212.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  213.  * external symbol (1), or was it fully resolved upon entering the
  214.  * loader (0) in which case some combination of the value in memory
  215.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  216.  * what the value of the relocation actually was.  *Must be an lvalue*.
  217.  *
  218.  *   RELOC_TYPE (rval): If this entry was fully resolved upon
  219.  * entering the loader, what type should it be relocated as?
  220.  *
  221.  *   RELOC_SYMBOL (rval): If this entry was not fully resolved upon
  222.  * entering the loader, what is the index of it's symbol in the symbol
  223.  * table?  *Must be a lvalue*.
  224.  *
  225.  *   RELOC_MEMORY_ADD_P (rval): This should return true if the final
  226.  * relocation value output here should be added to memory, or if the
  227.  * section of memory described should simply be set to the relocation
  228.  * value.
  229.  *
  230.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  231.  * an extra value to be added to the relocation value based on the
  232.  * individual relocation entry.  *Must be an lvalue if defined*.
  233.  *
  234.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  235.  * pc relative.
  236.  *
  237.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  238.  * final relocation value before putting it where it belongs.
  239.  *
  240.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  241.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  242.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  243.  * do everything in terms of the bit operators below), but having this
  244.  * macro could end up producing better code on machines without fancy
  245.  * bit twiddling.  Also, it's easier to understand/code big/little
  246.  * endian distinctions with this macro.
  247.  *
  248.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  249.  * object described in RELOC_TARGET_SIZE in which the relocation value
  250.  * will go.
  251.  *
  252.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  253.  * with the bits of the relocation value.  It may be assumed by the
  254.  * code that the relocation value will fit into this many bits.  This
  255.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  256.  *
  257.  *
  258.  *        Things I haven't implemented
  259.  *        ----------------------------
  260.  *
  261.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  262.  *
  263.  *    Pc relative relocation for External references.
  264.  *
  265.  *
  266.  */
  267.  
  268. /* The following #if has been modifed for cross compilation */
  269. /* It originally read:  #if defined(sun) && defined(sparc)  */
  270. /* Marc Ullman, Stanford University    Nov. 1 1989  */
  271. #if defined(sun) && (TARGET == SUN4)
  272. /* Sparc (Sun 4) macros */
  273. #undef relocation_info
  274. #define relocation_info                    reloc_info_sparc
  275. #define RELOC_ADDRESS(r)        ((r)->r_address)
  276. #define RELOC_EXTERN_P(r)               ((r)->r_extern)
  277. #define RELOC_TYPE(r)                   ((r)->r_index)
  278. #define RELOC_SYMBOL(r)                 ((r)->r_index)
  279. #define RELOC_MEMORY_SUB_P(r)        0
  280. #define RELOC_MEMORY_ADD_P(r)           0
  281. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)
  282. #define RELOC_PCREL_P(r)             \
  283.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  284. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  285. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  286. #define RELOC_TARGET_BITPOS(r)          0
  287. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  288.  
  289. /* Note that these are very dependent on the order of the enums in
  290.    enum reloc_type (in a.out.h); if they change the following must be
  291.    changed */
  292. /* Also note that the last few may be incorrect; I have no information */
  293. static int reloc_target_rightshift[] = {
  294.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  295. };
  296. static int reloc_target_size[] = {
  297.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  298. };
  299. static int reloc_target_bitsize[] = {
  300.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  301. };
  302.  
  303. #define    MAX_ALIGNMENT    (sizeof (double))
  304. #endif
  305. #ifdef sequent
  306. #define RELOC_ADDRESS(r)        ((r)->r_address)
  307. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  308. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  309. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  310. #define RELOC_MEMORY_SUB_P(r)    ((r)->r_bsr)
  311. #define RELOC_MEMORY_ADD_P(r)    1
  312. #undef RELOC_ADD_EXTRA
  313. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  314. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  315. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  316. #define RELOC_TARGET_BITPOS(r)    0
  317. #define RELOC_TARGET_BITSIZE(r)    32
  318. #endif
  319.  
  320. /* Default macros */
  321. #ifndef RELOC_ADDRESS
  322. #define RELOC_ADDRESS(r)        ((r)->r_address)
  323. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  324. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  325. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  326. #define RELOC_MEMORY_SUB_P(r)    0
  327. #define RELOC_MEMORY_ADD_P(r)    1
  328. #undef RELOC_ADD_EXTRA
  329. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  330. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  331. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  332. #define RELOC_TARGET_BITPOS(r)    0
  333. #define RELOC_TARGET_BITSIZE(r)    32
  334. #endif
  335.  
  336. #ifndef MAX_ALIGNMENT
  337. #define    MAX_ALIGNMENT    (sizeof (int))
  338. #endif
  339.  
  340. #ifdef nounderscore
  341. #define LPREFIX '.'
  342. #else
  343. #define LPREFIX 'L'
  344. #endif
  345.  
  346.  
  347. /* Special global symbol types understood by GNU LD.  */
  348.  
  349. /* The following type indicates the definition of a symbol as being
  350.    an indirect reference to another symbol.  The other symbol
  351.    appears as an undefined reference, immediately following this symbol.
  352.  
  353.    Indirection is asymmetrical.  The other symbol's value will be used
  354.    to satisfy requests for the indirect symbol, but not vice versa.
  355.    If the other symbol does not have a definition, libraries will
  356.    be searched to find a definition.
  357.  
  358.    So, for example, the following two lines placed in an assembler
  359.    input file would result in an object file which would direct gnu ld
  360.    to resolve all references to symbol "foo" as references to symbol
  361.    "bar".
  362.  
  363.     .stabs "_foo",11,0,0,0
  364.     .stabs "_bar",1,0,0,0
  365.  
  366.    Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)).  */
  367.  
  368. #ifndef N_INDR
  369. #define N_INDR 0xa
  370. #endif
  371.  
  372. /* The following symbols refer to set elements.  These are expected
  373.    only in input to the loader; they should not appear in loader
  374.    output (unless relocatable output is requested).  To be recognized
  375.    by the loader, the input symbols must have their N_EXT bit set.
  376.    All the N_SET[ATDB] symbols with the same name form one set.  The
  377.    loader collects all of these elements at load time and outputs a
  378.    vector for each name.
  379.    Space (an array of 32 bit words) is allocated for the set in the
  380.    data section, and the n_value field of each set element value is
  381.    stored into one word of the array.
  382.    The first word of the array is the length of the set (number of
  383.    elements).  The last word of the vector is set to zero for possible
  384.    use by incremental loaders.  The array is ordered by the linkage
  385.    order; the first symbols which the linker encounters will be first
  386.    in the array.
  387.  
  388.    In C syntax this looks like:
  389.  
  390.     struct set_vector {
  391.       unsigned int length;
  392.       unsigned int vector[length];
  393.       unsigned int always_zero;
  394.     };
  395.  
  396.    Before being placed into the array, each element is relocated
  397.    according to its type.  This allows the loader to create an array
  398.    of pointers to objects automatically.  N_SETA type symbols will not
  399.    be relocated.
  400.  
  401.    The address of the set is made into an N_SETV symbol
  402.    whose name is the same as the name of the set.
  403.    This symbol acts like a N_DATA global symbol
  404.    in that it can satisfy undefined external references.
  405.  
  406.    For the purposes of determining whether or not to load in a library
  407.    file, set element definitions are not considered "real
  408.    definitions"; they will not cause the loading of a library
  409.    member.
  410.  
  411.    If relocatable output is requested, none of this processing is
  412.    done.  The symbols are simply relocated and passed through to the
  413.    output file.
  414.  
  415.    So, for example, the following three lines of assembler code
  416.    (whether in one file or scattered between several different ones)
  417.    will produce a three element vector (total length is five words;
  418.    see above), referenced by the symbol "_xyzzy", which will have the
  419.    addresses of the routines _init1, _init2, and _init3.
  420.  
  421.    *NOTE*: If symbolic addresses are used in the n_value field of the
  422.    defining .stabs, those symbols must be defined in the same file as
  423.    that containing the .stabs.
  424.  
  425.     .stabs "_xyzzy",23,0,0,_init1
  426.     .stabs "_xyzzy",23,0,0,_init2
  427.     .stabs "_xyzzy",23,0,0,_init3
  428.  
  429.    Note that (23 == (N_SETT | N_EXT)).  */
  430.  
  431. #ifndef N_SETA
  432. #define    N_SETA    0x14        /* Absolute set element symbol */
  433. #endif                /* This is input to LD, in a .o file.  */
  434.  
  435. #ifndef N_SETT
  436. #define    N_SETT    0x16        /* Text set element symbol */
  437. #endif                /* This is input to LD, in a .o file.  */
  438.  
  439. #ifndef N_SETD
  440. #define    N_SETD    0x18        /* Data set element symbol */
  441. #endif                /* This is input to LD, in a .o file.  */
  442.  
  443. #ifndef N_SETB
  444. #define    N_SETB    0x1A        /* Bss set element symbol */
  445. #endif                /* This is input to LD, in a .o file.  */
  446.  
  447. /* Macros dealing with the set element symbols defined in a.out.h */
  448. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  449. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  450.  
  451. #ifndef N_SETV
  452. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  453. #endif                /* This is output from LD.  */
  454.  
  455. /* If a this type of symbol is encountered, its name is a warning
  456.    message to print each time the symbol referenced by the next symbol
  457.    table entry is referenced.
  458.  
  459.    This feature may be used to allow backwards compatibility with
  460.    certain functions (eg. gets) but to discourage programmers from
  461.    their use.
  462.  
  463.    So if, for example, you wanted to have ld print a warning whenever
  464.    the function "gets" was used in their C program, you would add the
  465.    following to the assembler file in which gets is defined:
  466.  
  467.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  468.     .stabs "_gets",1,0,0,0
  469.  
  470.    These .stabs do not necessarily have to be in the same file as the
  471.    gets function, they simply must exist somewhere in the compilation.  */
  472.  
  473. #ifndef N_WARNING
  474. #define N_WARNING 0x1E        /* Warning message to print if symbol
  475.                    included */
  476. #endif                /* This is input to ld */
  477.  
  478. #ifndef __GNU_STAB__
  479.  
  480. /* Line number for the data section.  This is to be used to describe
  481.    the source location of a variable declaration.  */
  482. #ifndef N_DSLINE
  483. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  484. #endif
  485.  
  486. /* Line number for the bss section.  This is to be used to describe
  487.    the source location of a variable declaration.  */
  488. #ifndef N_BSLINE
  489. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  490. #endif
  491.  
  492. #endif /* not __GNU_STAB__ */
  493.  
  494. /* Symbol table */
  495.  
  496. /* Global symbol data is recorded in these structures,
  497.    one for each global symbol.
  498.    They are found via hashing in 'symtab', which points to a vector of buckets.
  499.    Each bucket is a chain of these structures through the link field.  */
  500.  
  501. typedef
  502.   struct glosym
  503.     {
  504.       /* Pointer to next symbol in this symbol's hash bucket.  */
  505.       struct glosym *link;
  506.       /* Name of this symbol.  */
  507.       char *name;
  508.       /* Value of this symbol as a global symbol.  */
  509.       long value;
  510.       /* Chain of external 'nlist's in files for this symbol, both defs
  511.      and refs.  */
  512.       struct nlist *refs;
  513.       /* Any warning message that might be associated with this symbol
  514.          from an N_WARNING symbol encountered. */
  515.       char *warning;
  516.       /* Nonzero means definitions of this symbol as common have been seen,
  517.      and the value here is the largest size specified by any of them.  */
  518.       int max_common_size;
  519.       /* For relocatable_output, records the index of this global sym in the
  520.      symbol table to be written, with the first global sym given index 0.*/
  521.       int def_count;
  522.       /* Nonzero means a definition of this global symbol is known to exist.
  523.      Library members should not be loaded on its account.  */
  524.       char defined;
  525.       /* Nonzero means a reference to this global symbol has been seen
  526.      in a file that is surely being loaded.
  527.      A value higher than 1 is the n_type code for the symbol's
  528.      definition.  */
  529.       char referenced;
  530.       /* A count of the number of undefined references printed for a
  531.      specific symbol.  If a symbol is unresolved at the end of
  532.      digest_symbols (and the loading run is supposed to produce
  533.      relocatable output) do_file_warnings keeps track of how many
  534.      unresolved reference error messages have been printed for
  535.      each symbol here.  When the number hits MAX_UREFS_PRINTED,
  536.      messages stop. */
  537.       unsigned char undef_refs;
  538.       /* 1 means that this symbol has multiple definitions.  2 means
  539.          that it has multiple definitions, and some of them are set
  540.      elements, one of which has been printed out already.  */
  541.       unsigned char multiply_defined;
  542.       /* Nonzero means print a message at all refs or defs of this symbol */
  543.       char trace;
  544.     }
  545.   symbol;
  546.  
  547. /* Demangler for C++. */
  548. extern char *cplus_demangle ();
  549.  
  550. /* Demangler function to use. */
  551. char *(*demangler)() = NULL;
  552.  
  553. /* Number of buckets in symbol hash table */
  554. #define    TABSIZE    1009
  555.  
  556. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  557. symbol *symtab[TABSIZE];
  558.  
  559. /* Number of symbols in symbol hash table. */
  560. int num_hash_tab_syms = 0;
  561.  
  562. /* Count the number of nlist entries that are for local symbols.
  563.    This count and the three following counts
  564.    are incremented as as symbols are entered in the symbol table.  */
  565. int local_sym_count;
  566.  
  567. /* Count number of nlist entries that are for local symbols
  568.    whose names don't start with L. */
  569. int non_L_local_sym_count;
  570.  
  571. /* Count the number of nlist entries for debugger info.  */
  572. int debugger_sym_count;
  573.  
  574. /* Count the number of global symbols referenced and not defined.  */
  575. int undefined_global_sym_count;
  576.  
  577. /* Count the number of global symbols multiply defined.  */
  578. int multiple_def_count;
  579.  
  580. /* Count the number of defined global symbols.
  581.    Each symbol is counted only once
  582.    regardless of how many different nlist entries refer to it,
  583.    since the output file will need only one nlist entry for it.
  584.    This count is computed by `digest_symbols';
  585.    it is undefined while symbols are being loaded. */
  586. int defined_global_sym_count;
  587.  
  588. /* Count the number of symbols defined through common declarations.
  589.    This count is kept in symdef_library, linear_library, and
  590.    enter_global_ref.  It is incremented when the defined flag is set
  591.    in a symbol because of a common definition, and decremented when
  592.    the symbol is defined "for real" (ie. by something besides a common
  593.    definition).  */
  594. int common_defined_global_count;
  595.  
  596. /* Count the number of set element type symbols and the number of
  597.    separate vectors which these symbols will fit into.  See the
  598.    GNU a.out.h for more info.
  599.    This count is computed by 'enter_file_symbols' */
  600. int set_symbol_count;
  601. int set_vector_count;
  602.  
  603. /* Define a linked list of strings which define symbols which should
  604.    be treated as set elements even though they aren't.  Any symbol
  605.    with a prefix matching one of these should be treated as a set
  606.    element.
  607.  
  608.    This is to make up for deficiencies in many assemblers which aren't
  609.    willing to pass any stabs through to the loader which they don't
  610.    understand.  */
  611. struct string_list_element {
  612.   char *str;
  613.   struct string_list_element *next;
  614. };
  615.  
  616. struct string_list_element *set_element_prefixes;
  617.  
  618. /* Count the number of definitions done indirectly (ie. done relative
  619.    to the value of some other symbol. */
  620. int global_indirect_count;
  621.  
  622. /* Count the number of warning symbols encountered. */
  623. int warning_count;
  624.  
  625. /* Total number of symbols to be written in the output file.
  626.    Computed by digest_symbols from the variables above.  */
  627. int nsyms;
  628.  
  629.  
  630. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  631.    -e sets this.  */
  632. symbol *entry_symbol;
  633.  
  634. symbol *edata_symbol;   /* the symbol _edata */
  635. symbol *etext_symbol;   /* the symbol _etext */
  636. symbol *end_symbol;    /* the symbol _end */
  637.  
  638. /* Each input file, and each library member ("subfile") being loaded,
  639.    has a `file_entry' structure for it.
  640.  
  641.    For files specified by command args, these are contained in the vector
  642.    which `file_table' points to.
  643.  
  644.    For library members, they are dynamically allocated,
  645.    and chained through the `chain' field.
  646.    The chain is found in the `subfiles' field of the `file_entry'.
  647.    The `file_entry' objects for the members have `superfile' fields pointing
  648.    to the one for the library.  */
  649.  
  650. struct file_entry {
  651.   /* Name of this file.  */
  652.   char *filename;
  653.   /* Name to use for the symbol giving address of text start */
  654.   /* Usually the same as filename, but for a file spec'd with -l
  655.      this is the -l switch itself rather than the filename.  */
  656.   char *local_sym_name;
  657.  
  658.   /* Describe the layout of the contents of the file */
  659.  
  660.   /* The file's a.out header.  */
  661.   struct exec header;
  662.   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  663.   int symseg_offset;
  664.  
  665.   /* Describe data from the file loaded into core */
  666.  
  667.   /* Symbol table of the file.  */
  668.   struct nlist *symbols;
  669.   /* Size in bytes of string table.  */
  670.   int string_size;
  671.   /* Pointer to the string table.
  672.      The string table is not kept in core all the time,
  673.      but when it is in core, its address is here.  */
  674.   char *strings;
  675.  
  676.   /* Next two used only if `relocatable_output' or if needed for */
  677.   /* output of undefined reference line numbers. */
  678.  
  679.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  680.   struct relocation_info *textrel;
  681.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  682.   struct relocation_info *datarel;
  683.  
  684.   /* Relation of this file's segments to the output file */
  685.  
  686.   /* Start of this file's text seg in the output file core image.  */
  687.   int text_start_address;
  688.   /* Start of this file's data seg in the output file core image.  */
  689.   int data_start_address;
  690.   /* Start of this file's bss seg in the output file core image.  */
  691.   int bss_start_address;
  692.   /* Offset in bytes in the output file symbol table
  693.      of the first local symbol for this file.  Set by `write_file_symbols'.  */
  694.   int local_syms_offset;
  695.  
  696.   /* For library members only */
  697.  
  698.   /* For a library, points to chain of entries for the library members.  */
  699.   struct file_entry *subfiles;
  700.   /* For a library member, offset of the member within the archive.
  701.      Zero for files that are not library members.  */
  702.   int starting_offset;
  703.   /* Size of contents of this file, if library member.  */
  704.   int total_size;
  705.   /* For library member, points to the library's own entry.  */
  706.   struct file_entry *superfile;
  707.   /* For library member, points to next entry for next member.  */
  708.   struct file_entry *chain;
  709.  
  710.   /* 1 if file is a library. */
  711.   char library_flag;
  712.  
  713.   /* 1 if file's header has been read into this structure.  */
  714.   char header_read_flag;
  715.  
  716.   /* 1 means search a set of directories for this file.  */
  717.   char search_dirs_flag;
  718.  
  719.   /* 1 means this is base file of incremental load.
  720.      Do not load this file's text or data.
  721.      Also default text_start to after this file's bss. */
  722.   char just_syms_flag;
  723. };
  724.  
  725. /* Vector of entries for input files specified by arguments.
  726.    These are all the input files except for members of specified libraries.  */
  727. struct file_entry *file_table;
  728.  
  729. /* Length of that vector.  */
  730. int number_of_files;
  731.  
  732. /* When loading the text and data, we can avoid doing a close
  733.    and another open between members of the same library.
  734.  
  735.    These two variables remember the file that is currently open.
  736.    Both are zero if no file is open.
  737.  
  738.    See `each_file' and `file_close'.  */
  739.  
  740. struct file_entry *input_file;
  741. int input_desc;
  742.  
  743. /* The name of the file to write; "a.out" by default.  */
  744.  
  745. char *output_filename;
  746.  
  747. /* Descriptor for writing that file with `mywrite'.  */
  748.  
  749. int outdesc;
  750.  
  751. /* Header for that file (filled in by `write_header').  */
  752.  
  753. struct exec outheader;
  754.  
  755. #ifdef COFF_ENCAPSULATE
  756. struct coffheader coffheader;
  757. int need_coff_header;
  758. #endif
  759.  
  760. /* The following are computed by `digest_symbols'.  */
  761.  
  762. int text_size;        /* total size of text of all input files.  */
  763. int data_size;        /* total size of data of all input files.  */
  764. int bss_size;        /* total size of bss of all input files.  */
  765. int text_reloc_size;    /* total size of text relocation of all input files.  */
  766. int data_reloc_size;    /* total size of data relocation of all input */
  767.             /* files.  */
  768.  
  769. /* Specifications of start and length of the area reserved at the end
  770.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  771. int set_sect_start;
  772. int set_sect_size;
  773.  
  774. /* Pointer for in core storage for the above vectors, before they are
  775.    written. */
  776. unsigned long *set_vectors;
  777.  
  778. /* Amount of cleared space to leave between the text and data segments.  */
  779.  
  780. int text_pad;
  781.  
  782. /* Amount of bss segment to include as part of the data segment.  */
  783.  
  784. int data_pad;
  785.  
  786. /* Format of __.SYMDEF:
  787.    First, a longword containing the size of the 'symdef' data that follows.
  788.    Second, zero or more 'symdef' structures.
  789.    Third, a longword containing the length of symbol name strings.
  790.    Fourth, zero or more symbol name strings (each followed by a null).  */
  791.  
  792. struct symdef {
  793.   int symbol_name_string_index;
  794.   int library_member_offset;
  795. };
  796.  
  797. /* Record most of the command options.  */
  798.  
  799. /* Address we assume the text section will be loaded at.
  800.    We relocate symbols and text and data for this, but we do not
  801.    write any padding in the output file for it.  */
  802. int text_start;
  803.  
  804. /* Offset of default entry-pc within the text section.  */
  805. int entry_offset;
  806.  
  807. /* Address we decide the data section will be loaded at.  */
  808. int data_start;
  809.  
  810. /* `text-start' address is normally this much plus a page boundary.
  811.    This is not a user option; it is fixed for each system.  */
  812. int text_start_alignment;
  813.  
  814. /* Nonzero if -T was specified in the command line.
  815.    This prevents text_start from being set later to default values.  */
  816. int T_flag_specified;
  817.  
  818. /* Nonzero if -Tdata was specified in the command line.
  819.    This prevents data_start from being set later to default values.  */
  820. int Tdata_flag_specified;
  821.  
  822. /* Size to pad data section up to.
  823.    We simply increase the size of the data section, padding with zeros,
  824.    and reduce the size of the bss section to match.  */
  825. int specified_data_size;
  826.  
  827. /* Magic number to use for the output file, set by switch.  */
  828. int magic;
  829.  
  830. /* Nonzero means print names of input files as processed.  */
  831. int trace_files;
  832.  
  833. /* Which symbols should be stripped (omitted from the output):
  834.    none, all, or debugger symbols.  */
  835. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  836.  
  837. /* Which local symbols should be omitted:
  838.    none, all, or those starting with L.
  839.    This is irrelevant if STRIP_NONE.  */
  840. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  841.  
  842. /* 1 => write load map.  */
  843. int write_map;
  844.  
  845. /* 1 => write relocation into output file so can re-input it later.  */
  846. int relocatable_output;
  847.  
  848. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  849. int force_common_definition;
  850.  
  851. /* Standard directories to search for files specified by -l.  */
  852. char *standard_search_dirs[] =
  853. #ifdef STANDARD_SEARCH_DIRS
  854.   {STANDARD_SEARCH_DIRS};
  855. #else
  856. #ifdef NON_NATIVE
  857.   {"/usr/local/lib/gnu"};
  858. #else
  859.   {"/lib", "/usr/lib", "/usr/local/lib"};
  860. #endif
  861. #endif
  862.  
  863. /* Actual vector of directories to search;
  864.    this contains those specified with -L plus the standard ones.  */
  865. char **search_dirs;
  866.  
  867. /* Length of the vector `search_dirs'.  */
  868. int n_search_dirs;
  869.  
  870. /* Non zero means to create the output executable. */
  871. /* Cleared by nonfatal errors.  */
  872. int make_executable;
  873.  
  874. /* Force the executable to be output, even if there are non-fatal
  875.    errors */
  876. int force_executable;
  877.  
  878. /* Keep a list of any symbols referenced from the command line (so
  879.    that error messages for these guys can be generated). This list is
  880.    zero terminated. */
  881. struct glosym **cmdline_references;
  882. int cl_refs_allocated;
  883.  
  884. void bcopy (), bzero ();
  885. int malloc (), realloc ();
  886. #ifndef alloca
  887. int alloca ();
  888. #endif
  889. int free ();
  890.  
  891. int xmalloc ();
  892. int xrealloc ();
  893. void fatal ();
  894. void fatal_with_file ();
  895. void perror_name ();
  896. void perror_file ();
  897. void error ();
  898.  
  899. void digest_symbols ();
  900. void print_symbols ();
  901. void load_symbols ();
  902. void decode_command ();
  903. void list_undefined_symbols ();
  904. void list_unresolved_references ();
  905. void write_output ();
  906. void write_header ();
  907. void write_text ();
  908. void read_file_relocation ();
  909. void write_data ();
  910. void write_rel ();
  911. void write_syms ();
  912. void write_symsegs ();
  913. void mywrite ();
  914. void symtab_init ();
  915. void padfile ();
  916. char *concat ();
  917. char *get_file_name ();
  918. symbol *getsym (), *getsym_soft ();
  919.  
  920. int
  921. main (argc, argv)
  922.      char **argv;
  923.      int argc;
  924. {
  925. /*   Added this to stop ld core-dumping on very large .o files.    */
  926. #ifdef RLIMIT_STACK
  927.   /* Get rid of any avoidable limit on stack size.  */
  928.   {
  929.     struct rlimit rlim;
  930.  
  931.     /* Set the stack limit huge so that alloca does not fail. */
  932.     getrlimit (RLIMIT_STACK, &rlim);
  933.     rlim.rlim_cur = rlim.rlim_max;
  934.     setrlimit (RLIMIT_STACK, &rlim);
  935.   }
  936. #endif /* RLIMIT_STACK */
  937.  
  938.   page_size = getpagesize ();
  939.   progname = argv[0];
  940.  
  941.   /* Clear the cumulative info on the output file.  */
  942.  
  943.   text_size = 0;
  944.   data_size = 0;
  945.   bss_size = 0;
  946.   text_reloc_size = 0;
  947.   data_reloc_size = 0;
  948.  
  949.   data_pad = 0;
  950.   text_pad = 0;
  951.  
  952.   /* Initialize the data about options.  */
  953.  
  954.   specified_data_size = 0;
  955.   strip_symbols = STRIP_NONE;
  956.   trace_files = 0;
  957.   discard_locals = DISCARD_NONE;
  958.   entry_symbol = 0;
  959.   write_map = 0;
  960.   relocatable_output = 0;
  961.   force_common_definition = 0;
  962.   T_flag_specified = 0;
  963.   Tdata_flag_specified = 0;
  964.   magic = DEFAULT_MAGIC;
  965.   make_executable = 1;
  966.   force_executable = 0;
  967.   set_element_prefixes = 0;
  968.  
  969.   /* Initialize the cumulative counts of symbols.  */
  970.  
  971.   local_sym_count = 0;
  972.   non_L_local_sym_count = 0;
  973.   debugger_sym_count = 0;
  974.   undefined_global_sym_count = 0;
  975.   set_symbol_count = 0;
  976.   set_vector_count = 0;
  977.   global_indirect_count = 0;
  978.   warning_count = 0;
  979.   multiple_def_count = 0;
  980.   common_defined_global_count = 0;
  981.  
  982.   /* Keep a list of symbols referenced from the command line */
  983.   cl_refs_allocated = 10;
  984.   cmdline_references
  985.     = (struct glosym **) xmalloc (cl_refs_allocated
  986.                   * sizeof(struct glosym *));
  987.   *cmdline_references = 0;
  988.  
  989.   /* Completely decode ARGV.  */
  990.  
  991.   decode_command (argc, argv);
  992.  
  993.   /* Create the symbols `etext', `edata' and `end'.  */
  994.  
  995.   if (!relocatable_output)
  996.     symtab_init ();
  997.  
  998.   /* Determine whether to count the header as part of
  999.      the text size, and initialize the text size accordingly.
  1000.      This depends on the kind of system and on the output format selected.  */
  1001.  
  1002.   N_SET_MAGIC (outheader, magic);
  1003. #ifdef INITIALIZE_HEADER
  1004.   INITIALIZE_HEADER;
  1005. #endif
  1006.  
  1007.   text_size = sizeof (struct exec);
  1008. #ifdef COFF_ENCAPSULATE
  1009.   if (relocatable_output == 0 && file_table[0].just_syms_flag == 0)
  1010.     {
  1011.       need_coff_header = 1;
  1012.       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  1013.       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  1014.       text_size += sizeof (struct coffheader);
  1015.     }
  1016. #endif
  1017.  
  1018.   text_size -= N_TXTOFF (outheader);
  1019.  
  1020.   if (text_size < 0)
  1021.     text_size = 0;
  1022.   entry_offset = text_size;
  1023.  
  1024.   if (!T_flag_specified && !relocatable_output)
  1025.     text_start = N_TXTADDR (outheader);
  1026.  
  1027.   /* The text-start address is normally this far past a page boundary.  */
  1028.   text_start_alignment = text_start % page_size;
  1029.  
  1030.   /* Load symbols of all input files.
  1031.      Also search all libraries and decide which library members to load.  */
  1032.  
  1033.   load_symbols ();
  1034.  
  1035.   /* Compute where each file's sections go, and relocate symbols.  */
  1036.  
  1037.   digest_symbols ();
  1038.  
  1039.   /* Print error messages for any missing symbols, for any warning
  1040.      symbols, and possibly multiple definitions */
  1041.  
  1042.   do_warnings (stderr);
  1043.  
  1044.   /* Print a map, if requested.  */
  1045.  
  1046.   if (write_map) print_symbols (stdout);
  1047.  
  1048.   /* Write the output file.  */
  1049.  
  1050.   if (make_executable || force_executable)
  1051.     write_output ();
  1052.  
  1053.   exit (!make_executable);
  1054. }
  1055.  
  1056. void decode_option ();
  1057.  
  1058. /* Analyze a command line argument.
  1059.    Return 0 if the argument is a filename.
  1060.    Return 1 if the argument is a option complete in itself.
  1061.    Return 2 if the argument is a option which uses an argument.
  1062.  
  1063.    Thus, the value is the number of consecutive arguments
  1064.    that are part of options.  */
  1065.  
  1066. int
  1067. classify_arg (arg)
  1068.      register char *arg;
  1069. {
  1070.   if (*arg != '-') return 0;
  1071.   switch (arg[1])
  1072.     {
  1073.     case 'A':
  1074.     case 'D':
  1075.     case 'e':
  1076.     case 'L':
  1077.     case 'l':
  1078.     case 'o':
  1079.     case 'u':
  1080.     case 'V':
  1081.     case 'y':
  1082.       if (arg[2])
  1083.     return 1;
  1084.       return 2;
  1085.  
  1086.     case 'B':
  1087.       if (! strcmp (&arg[2], "static"))
  1088.     return 1;
  1089.  
  1090.     case 'T':
  1091.       if (arg[2] == 0)
  1092.     return 2;
  1093.       if (! strcmp (&arg[2], "text"))
  1094.     return 2;
  1095.       if (! strcmp (&arg[2], "data"))
  1096.     return 2;
  1097.       return 1;
  1098.     }
  1099.  
  1100.   return 1;
  1101. }
  1102.  
  1103. /* Process the command arguments,
  1104.    setting up file_table with an entry for each input file,
  1105.    and setting variables according to the options.  */
  1106.  
  1107. void
  1108. decode_command (argc, argv)
  1109.      char **argv;
  1110.      int argc;
  1111. {
  1112.   register int i;
  1113.   register struct file_entry *p;
  1114.  
  1115.   number_of_files = 0;
  1116.   output_filename = "a.out";
  1117.  
  1118.   n_search_dirs = 0;
  1119.   search_dirs = (char **) xmalloc (sizeof (char *));
  1120.  
  1121.   /* First compute number_of_files so we know how long to make file_table.  */
  1122.   /* Also process most options completely.  */
  1123.  
  1124.   for (i = 1; i < argc; i++)
  1125.     {
  1126.       register int code = classify_arg (argv[i]);
  1127.       if (code)
  1128.     {
  1129.       if (i + code > argc)
  1130.         fatal ("no argument following %s\n", argv[i]);
  1131.  
  1132.       decode_option (argv[i], argv[i+1]);
  1133.  
  1134.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  1135.         number_of_files++;
  1136.  
  1137.       i += code - 1;
  1138.     }
  1139.       else
  1140.     number_of_files++;
  1141.     }
  1142.  
  1143.   if (!number_of_files)
  1144.     fatal ("no input files", 0);
  1145.  
  1146.   p = file_table
  1147.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1148.   bzero (p, number_of_files * sizeof (struct file_entry));
  1149.  
  1150.   /* Now scan again and fill in file_table.  */
  1151.   /* All options except -A and -l are ignored here.  */
  1152.  
  1153.   for (i = 1; i < argc; i++)
  1154.     {
  1155.       register int code = classify_arg (argv[i]);
  1156.  
  1157.       if (code)
  1158.     {
  1159.       char *string;
  1160.       if (code == 2)
  1161.         string = argv[i+1];
  1162.       else
  1163.         string = &argv[i][2];
  1164.  
  1165.       if (argv[i][1] == 'A')
  1166.         {
  1167.           if (p != file_table)
  1168.         fatal ("-A specified before an input file other than the first");
  1169.  
  1170.           p->filename = string;
  1171.           p->local_sym_name = string;
  1172.           p->just_syms_flag = 1;
  1173.           p++;
  1174.         }
  1175.       if (argv[i][1] == 'l')
  1176.         {
  1177.           p->filename = concat ("lib", string, ".a");
  1178.           p->local_sym_name = concat ("-l", string, "");
  1179.           p->search_dirs_flag = 1;
  1180.           p++;
  1181.         }
  1182.       i += code - 1;
  1183.     }
  1184.       else
  1185.     {
  1186.       p->filename = argv[i];
  1187.       p->local_sym_name = argv[i];
  1188.       p++;
  1189.     }
  1190.     }
  1191.  
  1192.   /* Now check some option settings for consistency.  */
  1193.  
  1194. #ifdef NMAGIC
  1195.   if ((magic == ZMAGIC || magic == NMAGIC)
  1196. #else
  1197.   if ((magic == ZMAGIC)
  1198. #endif
  1199.       && (text_start - text_start_alignment) & (page_size - 1))
  1200.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  1201.  
  1202.   /* Append the standard search directories to the user-specified ones.  */
  1203.   {
  1204.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1205.     n_search_dirs += n;
  1206.     search_dirs
  1207.       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1208.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1209.        n * sizeof (char *));
  1210.   }
  1211. }
  1212.  
  1213.  
  1214. void
  1215. add_cmdline_ref (sp)
  1216.      struct glosym *sp;
  1217. {
  1218.   struct glosym **ptr;
  1219.  
  1220.   for (ptr = cmdline_references;
  1221.        ptr < cmdline_references + cl_refs_allocated && *ptr;
  1222.        ptr++)
  1223.     ;
  1224.  
  1225.   if (ptr >= cmdline_references + cl_refs_allocated - 1)
  1226.     {
  1227.       int diff = ptr - cmdline_references;
  1228.  
  1229.       cl_refs_allocated *= 2;
  1230.       cmdline_references = (struct glosym **)
  1231.     xrealloc (cmdline_references,
  1232.          cl_refs_allocated * sizeof (struct glosym *));
  1233.       ptr = cmdline_references + diff;
  1234.     }
  1235.  
  1236.   *ptr++ = sp;
  1237.   *ptr = (struct glosym *) 0;
  1238. }
  1239.  
  1240. int
  1241. set_element_prefixed_p (name)
  1242.      char *name;
  1243. {
  1244.   struct string_list_element *p;
  1245.   int i;
  1246.  
  1247.   for (p = set_element_prefixes; p; p = p->next)
  1248.     {
  1249.       for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
  1250.     ;
  1251.  
  1252.       if (p->str[i] == '\0')
  1253.     return 1;
  1254.     }
  1255.   return 0;
  1256. }
  1257.  
  1258. int parse ();
  1259.  
  1260. /* Record an option and arrange to act on it later.
  1261.    ARG should be the following command argument,
  1262.    which may or may not be used by this option.
  1263.  
  1264.    The `l' and `A' options are ignored here since they actually
  1265.    specify input files.  */
  1266.  
  1267. void
  1268. decode_option (swt, arg)
  1269.      register char *swt, *arg;
  1270. {
  1271.   /* We get Bstatic from gcc on suns.  */
  1272.   if (! strcmp (swt + 1, "Bstatic"))
  1273.     return;
  1274.   if (! strcmp (swt + 1, "Ttext"))
  1275.     {
  1276.       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1277.       T_flag_specified = 1;
  1278.       return;
  1279.     }
  1280.   if (! strcmp (swt + 1, "Tdata"))
  1281.     {
  1282.       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1283.       Tdata_flag_specified = 1;
  1284.       return;
  1285.     }
  1286.   if (! strcmp (swt + 1, "noinhibit-exec"))
  1287.     {
  1288.       force_executable = 1;
  1289.       return;
  1290.     }
  1291.  
  1292.   if (swt[2] != 0)
  1293.     arg = &swt[2];
  1294.  
  1295.   switch (swt[1])
  1296.     {
  1297.     case 'A':
  1298.       return;
  1299.  
  1300.     case 'D':
  1301.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1302.       return;
  1303.  
  1304.     case 'd':
  1305.       force_common_definition = 1;
  1306.       return;
  1307.  
  1308.     case 'e':
  1309.       entry_symbol = getsym (arg);
  1310.       if (!entry_symbol->defined && !entry_symbol->referenced)
  1311.     undefined_global_sym_count++;
  1312.       entry_symbol->referenced = 1;
  1313.       add_cmdline_ref (entry_symbol);
  1314.       return;
  1315.  
  1316.     case 'l':
  1317.       /* If linking with libg++, use the C++ demangler. */
  1318.       if (arg != NULL && strcmp (arg, "g++") == 0)
  1319.     demangler = cplus_demangle;
  1320.       return;
  1321.  
  1322.     case 'L':
  1323.       n_search_dirs++;
  1324.       search_dirs
  1325.     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1326.       search_dirs[n_search_dirs - 1] = arg;
  1327.       return;
  1328.  
  1329.     case 'M':
  1330.       write_map = 1;
  1331.       return;
  1332.  
  1333.     case 'N':
  1334.       magic = OMAGIC;
  1335.       return;
  1336.  
  1337. #ifdef NMAGIC
  1338.     case 'n':
  1339.       magic = NMAGIC;
  1340.       return;
  1341. #endif
  1342.  
  1343.     case 'o':
  1344.       output_filename = arg;
  1345.       return;
  1346.  
  1347.     case 'r':
  1348.       relocatable_output = 1;
  1349.       magic = OMAGIC;
  1350.       text_start = 0;
  1351.       return;
  1352.  
  1353.     case 'S':
  1354.       strip_symbols = STRIP_DEBUGGER;
  1355.       return;
  1356.  
  1357.     case 's':
  1358.       strip_symbols = STRIP_ALL;
  1359.       return;
  1360.  
  1361.     case 'T':
  1362.       text_start = parse (arg, "%x", "invalid argument to -T");
  1363.       T_flag_specified = 1;
  1364.       return;
  1365.  
  1366.     case 't':
  1367.       trace_files = 1;
  1368.       return;
  1369.  
  1370.     case 'u':
  1371.       {
  1372.     register symbol *sp = getsym (arg);
  1373.     if (!sp->defined && !sp->referenced)
  1374.       undefined_global_sym_count++;
  1375.     sp->referenced = 1;
  1376.     add_cmdline_ref (sp);
  1377.       }
  1378.       return;
  1379.  
  1380.     case 'V':
  1381.       {
  1382.     struct string_list_element *new
  1383.       = (struct string_list_element *)
  1384.         xmalloc (sizeof (struct string_list_element));
  1385.  
  1386.     new->str = arg;
  1387.     new->next = set_element_prefixes;
  1388.     set_element_prefixes = new;
  1389.     return;
  1390.       }
  1391.  
  1392.     case 'X':
  1393.       discard_locals = DISCARD_L;
  1394.       return;
  1395.  
  1396.     case 'x':
  1397.       discard_locals = DISCARD_ALL;
  1398.       return;
  1399.  
  1400.     case 'y':
  1401.       {
  1402.     register symbol *sp = getsym (&swt[2]);
  1403.     sp->trace = 1;
  1404.       }
  1405.       return;
  1406.  
  1407.     case 'z':
  1408.       magic = ZMAGIC;
  1409.       return;
  1410.  
  1411.     default:
  1412.       fatal ("invalid command option `%s'", swt);
  1413.     }
  1414. }
  1415.  
  1416. /** Convenient functions for operating on one or all files being */
  1417.  /** loaded.  */
  1418. void print_file_name ();
  1419.  
  1420. /* Call FUNCTION on each input file entry.
  1421.    Do not call for entries for libraries;
  1422.    instead, call once for each library member that is being loaded.
  1423.  
  1424.    FUNCTION receives two arguments: the entry, and ARG.  */
  1425.  
  1426. void
  1427. each_file (function, arg)
  1428.      register void (*function)();
  1429.      register int arg;
  1430. {
  1431.   register int i;
  1432.  
  1433.   for (i = 0; i < number_of_files; i++)
  1434.     {
  1435.       register struct file_entry *entry = &file_table[i];
  1436.       if (entry->library_flag)
  1437.         {
  1438.       register struct file_entry *subentry = entry->subfiles;
  1439.       for (; subentry; subentry = subentry->chain)
  1440.         (*function) (subentry, arg);
  1441.     }
  1442.       else
  1443.     (*function) (entry, arg);
  1444.     }
  1445. }
  1446.  
  1447. /* Call FUNCTION on each input file entry until it returns a non-zero
  1448.    value.  Return this value.
  1449.    Do not call for entries for libraries;
  1450.    instead, call once for each library member that is being loaded.
  1451.  
  1452.    FUNCTION receives two arguments: the entry, and ARG.  It must be a
  1453.    function returning unsigned long (though this can probably be fudged). */
  1454.  
  1455. unsigned long
  1456. check_each_file (function, arg)
  1457.      register unsigned long (*function)();
  1458.      register int arg;
  1459. {
  1460.   register int i;
  1461.   register unsigned long return_val;
  1462.  
  1463.   for (i = 0; i < number_of_files; i++)
  1464.     {
  1465.       register struct file_entry *entry = &file_table[i];
  1466.       if (entry->library_flag)
  1467.         {
  1468.       register struct file_entry *subentry = entry->subfiles;
  1469.       for (; subentry; subentry = subentry->chain)
  1470.         if (return_val = (*function) (subentry, arg))
  1471.           return return_val;
  1472.     }
  1473.       else
  1474.     if (return_val = (*function) (entry, arg))
  1475.       return return_val;
  1476.     }
  1477.   return 0;
  1478. }
  1479.  
  1480. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  1481.  
  1482. void
  1483. each_full_file (function, arg)
  1484.      register void (*function)();
  1485.      register int arg;
  1486. {
  1487.   register int i;
  1488.  
  1489.   for (i = 0; i < number_of_files; i++)
  1490.     {
  1491.       register struct file_entry *entry = &file_table[i];
  1492.       if (entry->just_syms_flag)
  1493.     continue;
  1494.       if (entry->library_flag)
  1495.         {
  1496.       register struct file_entry *subentry = entry->subfiles;
  1497.       for (; subentry; subentry = subentry->chain)
  1498.         (*function) (subentry, arg);
  1499.     }
  1500.       else
  1501.     (*function) (entry, arg);
  1502.     }
  1503. }
  1504.  
  1505. /* Close the input file that is now open.  */
  1506.  
  1507. void
  1508. file_close ()
  1509. {
  1510.   close (input_desc);
  1511.   input_desc = 0;
  1512.   input_file = 0;
  1513. }
  1514.  
  1515. /* Open the input file specified by 'entry', and return a descriptor.
  1516.    The open file is remembered; if the same file is opened twice in a row,
  1517.    a new open is not actually done.  */
  1518.  
  1519. int
  1520. file_open (entry)
  1521.      register struct file_entry *entry;
  1522. {
  1523.   register int desc;
  1524.  
  1525.   if (entry->superfile)
  1526.     return file_open (entry->superfile);
  1527.  
  1528.   if (entry == input_file)
  1529.     return input_desc;
  1530.  
  1531.   if (input_file) file_close ();
  1532.  
  1533.   if (entry->search_dirs_flag)
  1534.     {
  1535.       int i;
  1536.  
  1537.       for (i = 0; i < n_search_dirs; i++)
  1538.     {
  1539.       register char *string
  1540.         = concat (search_dirs[i], "/", entry->filename);
  1541.       desc = open (string, O_RDONLY, 0);
  1542.       if (desc > 0)
  1543.         {
  1544.           entry->filename = string;
  1545.           entry->search_dirs_flag = 0;
  1546.           break;
  1547.         }
  1548.       free (string);
  1549.     }
  1550.     }
  1551.   else
  1552.     desc = open (entry->filename, O_RDONLY, 0);
  1553.  
  1554.   if (desc > 0)
  1555.     {
  1556.       input_file = entry;
  1557.       input_desc = desc;
  1558.       return desc;
  1559.     }
  1560.  
  1561.   perror_file (entry);
  1562.   /* NOTREACHED */
  1563. }
  1564.  
  1565. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1566.    and then a newline.  */
  1567.  
  1568. void
  1569. prline_file_name (entry, outfile)
  1570.      struct file_entry *entry;
  1571.      FILE *outfile;
  1572. {
  1573.   print_file_name (entry, outfile);
  1574.   fprintf (outfile, "\n");
  1575. }
  1576.  
  1577. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1578.  
  1579. void
  1580. print_file_name (entry, outfile)
  1581.      struct file_entry *entry;
  1582.      FILE *outfile;
  1583. {
  1584.   if (entry->superfile)
  1585.     {
  1586.       print_file_name (entry->superfile, outfile);
  1587.       fprintf (outfile, "(%s)", entry->filename);
  1588.     }
  1589.   else
  1590.     fprintf (outfile, "%s", entry->filename);
  1591. }
  1592.  
  1593. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1594.  
  1595. char *
  1596. get_file_name (entry)
  1597.      struct file_entry *entry;
  1598. {
  1599.   char *result, *supfile;
  1600.   if (entry->superfile)
  1601.     {
  1602.       supfile = get_file_name (entry->superfile);
  1603.       result = (char *) xmalloc (strlen (supfile)
  1604.                  + strlen (entry->filename) + 3);
  1605.       sprintf (result, "%s(%s)", supfile, entry->filename);
  1606.       free (supfile);
  1607.     }
  1608.   else
  1609.     {
  1610.       result = (char *) xmalloc (strlen (entry->filename) + 1);
  1611.       strcpy (result, entry->filename);
  1612.     }
  1613.   return result;
  1614. }
  1615.  
  1616. /* Medium-level input routines for rel files.  */
  1617.  
  1618. /* Read a file's header into the proper place in the file_entry.
  1619.    DESC is the descriptor on which the file is open.
  1620.    ENTRY is the file's entry.  */
  1621.  
  1622. void
  1623. read_header (desc, entry)
  1624.      int desc;
  1625.      register struct file_entry *entry;
  1626. {
  1627.   register int len;
  1628.   struct exec *loc = (struct exec *) &entry->header;
  1629.  
  1630.   lseek (desc, entry->starting_offset, 0);
  1631. #ifdef COFF_ENCAPSULATE
  1632.   if (entry->just_syms_flag)
  1633.     lseek (desc, sizeof(coffheader), 1);
  1634. #endif
  1635.   len = read (desc, loc, sizeof (struct exec));
  1636.   if (len != sizeof (struct exec))
  1637.     fatal_with_file ("failure reading header of ", entry);
  1638.   if (N_BADMAG (*loc))
  1639.     fatal_with_file ("bad magic number in ", entry);
  1640.  
  1641.   entry->header_read_flag = 1;
  1642. }
  1643.  
  1644. /* Read the symbols of file ENTRY into core.
  1645.    Assume it is already open, on descriptor DESC.
  1646.    Also read the length of the string table, which follows the symbol table,
  1647.    but don't read the contents of the string table.  */
  1648.  
  1649. void
  1650. read_entry_symbols (desc, entry)
  1651.      struct file_entry *entry;
  1652.      int desc;
  1653. {
  1654.   int str_size;
  1655.  
  1656.   if (!entry->header_read_flag)
  1657.     read_header (desc, entry);
  1658.  
  1659.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1660.  
  1661.   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1662.   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1663.     fatal_with_file ("premature end of file in symbols of ", entry);
  1664.  
  1665.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1666.   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1667.     fatal_with_file ("bad string table size in ", entry);
  1668.  
  1669.   entry->string_size = str_size;
  1670. }
  1671.  
  1672. /* Read the string table of file ENTRY into core.
  1673.    Assume it is already open, on descriptor DESC.
  1674.    Also record whether a GDB symbol segment follows the string table.  */
  1675.  
  1676. void
  1677. read_entry_strings (desc, entry)
  1678.      struct file_entry *entry;
  1679.      int desc;
  1680. {
  1681.   int buffer;
  1682.  
  1683.   if (!entry->header_read_flag)
  1684.     read_header (desc, entry);
  1685.  
  1686.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1687.   if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1688.     fatal_with_file ("premature end of file in strings of ", entry);
  1689.  
  1690.   /* While we are here, see if the file has a symbol segment at the end.
  1691.      For a separate file, just try reading some more.
  1692.      For a library member, compare current pos against total size.  */
  1693.   if (entry->superfile)
  1694.     {
  1695.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1696.     return;
  1697.     }
  1698.   else
  1699.     {
  1700.       buffer = read (desc, &buffer, sizeof buffer);
  1701.       if (buffer == 0)
  1702.     return;
  1703.       if (buffer != sizeof buffer)
  1704.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1705.     }
  1706.   /* Don't try to do anything with symsegs.  */
  1707.   return;
  1708.  
  1709.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1710. }
  1711.  
  1712. /* Read in the symbols of all input files.  */
  1713.  
  1714. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1715. void enter_file_symbols (), enter_global_ref (), search_library ();
  1716.  
  1717. void
  1718. load_symbols ()
  1719. {
  1720.   register int i;
  1721.  
  1722.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  1723.  
  1724.   for (i = 0; i < number_of_files; i++)
  1725.     {
  1726.       register struct file_entry *entry = &file_table[i];
  1727.       read_file_symbols (entry);
  1728.     }
  1729.  
  1730.   if (trace_files) fprintf (stderr, "\n");
  1731. }
  1732.  
  1733. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1734.    If it is a library, search it and load the appropriate members
  1735.    (which means calling this function recursively on those members).  */
  1736.  
  1737. void
  1738. read_file_symbols (entry)
  1739.      register struct file_entry *entry;
  1740. {
  1741.   register int desc;
  1742.   register int len;
  1743.   struct exec hdr;
  1744.  
  1745.   desc = file_open (entry);
  1746.  
  1747. #ifdef COFF_ENCAPSULATE
  1748.   if (entry->just_syms_flag)
  1749.     lseek (desc, sizeof(coffheader),0);
  1750. #endif
  1751.  
  1752.   len = read (desc, &hdr, sizeof hdr);
  1753.   if (len != sizeof hdr)
  1754.     fatal_with_file ("failure reading header of ", entry);
  1755.  
  1756.   if (!N_BADMAG (hdr))
  1757.     {
  1758.       read_entry_symbols (desc, entry);
  1759.       entry->strings = (char *) alloca (entry->string_size);
  1760.       read_entry_strings (desc, entry);
  1761.       enter_file_symbols (entry);
  1762.       entry->strings = 0;
  1763.     }
  1764.   else
  1765.     {
  1766.       char armag[SARMAG];
  1767.  
  1768.       lseek (desc, 0, 0);
  1769.       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1770.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1771.       entry->library_flag = 1;
  1772.       search_library (desc, entry);
  1773.     }
  1774.  
  1775.   file_close ();
  1776. }
  1777.  
  1778. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  1779.  
  1780. void
  1781. enter_file_symbols (entry)
  1782.      struct file_entry *entry;
  1783. {
  1784.   register struct nlist
  1785.     *p,
  1786.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1787.  
  1788.   if (trace_files) prline_file_name (entry, stderr);
  1789.  
  1790.   for (p = entry->symbols; p < end; p++)
  1791.     {
  1792.       if (p->n_type == (N_SETV | N_EXT)) continue;
  1793.       if (set_element_prefixes
  1794.       && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
  1795.     p->n_type += (N_SETA - N_ABS);
  1796.  
  1797.       if (SET_ELEMENT_P (p->n_type))
  1798.     {
  1799.       set_symbol_count++;
  1800.       if (!relocatable_output)
  1801.         enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1802.     }
  1803.       else if (p->n_type == N_WARNING)
  1804.     {
  1805.       char *name = p->n_un.n_strx + entry->strings;
  1806.  
  1807.       /* Grab the next entry.  */
  1808.       p++;
  1809.       if (p->n_type != (N_UNDF | N_EXT))
  1810.         {
  1811.           fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
  1812.                progname, entry->filename);
  1813.           make_executable = 0;
  1814.           p--;        /* Process normally.  */
  1815.         }
  1816.       else
  1817.         {
  1818.           symbol *sp;
  1819.           char *sname = p->n_un.n_strx + entry->strings;
  1820.           /* Deal with the warning symbol.  */
  1821.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1822.           sp = getsym (sname);
  1823.           sp->warning = (char *) xmalloc (strlen(name) + 1);
  1824.           strcpy (sp->warning, name);
  1825.           warning_count++;
  1826.         }
  1827.     }
  1828.       else if (p->n_type & N_EXT)
  1829.     enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1830.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1831.     {
  1832.       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
  1833.         non_L_local_sym_count++;
  1834.       local_sym_count++;
  1835.     }
  1836.       else debugger_sym_count++;
  1837.     }
  1838.  
  1839.    /* Count one for the local symbol that we generate,
  1840.       whose name is the file's name (usually) and whose address
  1841.       is the start of the file's text.  */
  1842.  
  1843.   local_sym_count++;
  1844.   non_L_local_sym_count++;
  1845. }
  1846.  
  1847. /* Enter one global symbol in the hash table.
  1848.    NLIST_P points to the `struct nlist' read from the file
  1849.    that describes the global symbol.  NAME is the symbol's name.
  1850.    ENTRY is the file entry for the file the symbol comes from.
  1851.  
  1852.    The `struct nlist' is modified by placing it on a chain of
  1853.    all such structs that refer to the same global symbol.
  1854.    This chain starts in the `refs' field of the symbol table entry
  1855.    and is chained through the `n_name'.  */
  1856.  
  1857. void
  1858. enter_global_ref (nlist_p, name, entry)
  1859.      register struct nlist *nlist_p;
  1860.      char *name;
  1861.      struct file_entry *entry;
  1862. {
  1863.   register symbol *sp = getsym (name);
  1864.   register int type = nlist_p->n_type;
  1865.   int oldref = sp->referenced;
  1866.   int olddef = sp->defined;
  1867.  
  1868.   nlist_p->n_un.n_name = (char *) sp->refs;
  1869.   sp->refs = nlist_p;
  1870.  
  1871.   sp->referenced = 1;
  1872.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1873.     {
  1874.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  1875.     sp->defined = type;
  1876.  
  1877.       if (oldref && !olddef)
  1878.     /* It used to be undefined and we're defining it.  */
  1879.     undefined_global_sym_count--;
  1880.  
  1881.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  1882.     {
  1883.       /* First definition and it's common.  */
  1884.       common_defined_global_count++;
  1885.       sp->max_common_size = nlist_p->n_value;
  1886.     }
  1887.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  1888.     {
  1889.       /* It used to be common and we're defining it as
  1890.          something else.  */
  1891.       common_defined_global_count--;
  1892.       sp->max_common_size = 0;
  1893.     }
  1894.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  1895.       && sp->max_common_size < nlist_p->n_value)
  1896.     /* It used to be common and this is a new common entry to
  1897.        which we need to pay attention.  */
  1898.     sp->max_common_size = nlist_p->n_value;
  1899.  
  1900.       /* Are we defining it as a set element?  */
  1901.       if (SET_ELEMENT_P (type)
  1902.       && (!olddef || (olddef && sp->max_common_size)))
  1903.     set_vector_count++;
  1904.       /* As an indirection?  */
  1905.       else if (type == (N_INDR | N_EXT))
  1906.     {
  1907.       /* Indirect symbols value should be modified to point
  1908.          a symbol being equivalenced to. */
  1909.       nlist_p->n_value
  1910.         = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1911.                      + entry->strings);
  1912.       if ((symbol *) nlist_p->n_value == sp)
  1913.         {
  1914.           /* Somebody redefined a symbol to be itself.  */
  1915.           fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
  1916.                entry->filename, name);
  1917.           /* Rewrite this symbol as being a global text symbol
  1918.          with value 0.  */
  1919.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  1920.           nlist_p->n_value = 0;
  1921.           /* Don't make the output executable.  */
  1922.           make_executable = 0;
  1923.         }
  1924.       else
  1925.         global_indirect_count++;
  1926.     }
  1927.     }
  1928.   else
  1929.     if (!oldref)
  1930. #ifndef DOLLAR_KLUDGE
  1931.       undefined_global_sym_count++;
  1932. #else
  1933.       {
  1934.     if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  1935.       {
  1936.         /* This is an (ISI?) $-conditional; skip it */
  1937.         sp->referenced = 0;
  1938.         if (sp->trace)
  1939.           {
  1940.         fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  1941.         print_file_name (entry, stderr);
  1942.         fprintf (stderr, "\n");
  1943.           }
  1944.         return;
  1945.       }
  1946.     else
  1947.       undefined_global_sym_count++;
  1948.       }
  1949. #endif
  1950.  
  1951.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  1952.     text_start = nlist_p->n_value;
  1953.  
  1954.   if (sp->trace)
  1955.     {
  1956.       register char *reftype;
  1957.       switch (type & N_TYPE)
  1958.     {
  1959.     case N_UNDF:
  1960.       if (nlist_p->n_value)
  1961.         reftype = "defined as common";
  1962.       else reftype = "referenced";
  1963.       break;
  1964.  
  1965.     case N_ABS:
  1966.       reftype = "defined as absolute";
  1967.       break;
  1968.  
  1969.     case N_TEXT:
  1970.       reftype = "defined in text section";
  1971.       break;
  1972.  
  1973.     case N_DATA:
  1974.       reftype = "defined in data section";
  1975.       break;
  1976.  
  1977.     case N_BSS:
  1978.       reftype = "defined in BSS section";
  1979.       break;
  1980.  
  1981.     case N_SETT:
  1982.       reftype = "is a text set element";
  1983.       break;
  1984.  
  1985.     case N_SETD:
  1986.       reftype = "is a data set element";
  1987.       break;
  1988.  
  1989.     case N_SETB:
  1990.       reftype = "is a BSS set element";
  1991.       break;
  1992.  
  1993.     case N_SETA:
  1994.       reftype = "is an absolute set element";
  1995.       break;
  1996.  
  1997.     case N_SETV:
  1998.       reftype = "defined in data section as vector";
  1999.       break;
  2000.  
  2001.     case N_INDR:
  2002.       reftype = (char *) alloca (23
  2003.                      + strlen ((nlist_p + 1)->n_un.n_strx
  2004.                            + entry->strings));
  2005.       sprintf (reftype, "defined equivalent to %s",
  2006.            (nlist_p + 1)->n_un.n_strx + entry->strings);
  2007.       break;
  2008.  
  2009. #ifdef sequent
  2010.     case N_SHUNDF:
  2011.       reftype = "shared undf";
  2012.       break;
  2013.  
  2014. /* These conflict with cases above.
  2015.     case N_SHDATA:
  2016.       reftype = "shared data";
  2017.       break;
  2018.  
  2019.     case N_SHBSS:
  2020.       reftype = "shared BSS";
  2021.       break;
  2022. */
  2023.     default:
  2024.       reftype = "I don't know this type";
  2025.       break;
  2026. #endif
  2027.     }
  2028.  
  2029.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2030.       print_file_name (entry, stderr);
  2031.       fprintf (stderr, "\n");
  2032.     }
  2033. }
  2034.  
  2035. /* This return 0 if the given file entry's symbol table does *not*
  2036.    contain the nlist point entry, and it returns the files entry
  2037.    pointer (cast to unsigned long) if it does. */
  2038.  
  2039. unsigned long
  2040. contains_symbol (entry, n_ptr)
  2041.      struct file_entry *entry;
  2042.      register struct nlist *n_ptr;
  2043. {
  2044.   if (n_ptr >= entry->symbols &&
  2045.       n_ptr < (entry->symbols
  2046.            + (entry->header.a_syms / sizeof (struct nlist))))
  2047.     return (unsigned long) entry;
  2048.   return 0;
  2049. }
  2050.  
  2051.  
  2052. /* Searching libraries */
  2053.  
  2054. struct file_entry *decode_library_subfile ();
  2055. void linear_library (), symdef_library ();
  2056.  
  2057. /* Search the library ENTRY, already open on descriptor DESC.
  2058.    This means deciding which library members to load,
  2059.    making a chain of `struct file_entry' for those members,
  2060.    and entering their global symbols in the hash table.  */
  2061.  
  2062. void
  2063. search_library (desc, entry)
  2064.      int desc;
  2065.      struct file_entry *entry;
  2066. {
  2067.   int member_length;
  2068.   register char *name;
  2069.   register struct file_entry *subentry;
  2070.  
  2071.   if (!undefined_global_sym_count) return;
  2072.  
  2073.   /* Examine its first member, which starts SARMAG bytes in.  */
  2074.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  2075.   if (!subentry) return;
  2076.  
  2077.   name = subentry->filename;
  2078.   free (subentry);
  2079.  
  2080.   /* Search via __.SYMDEF if that exists, else linearly.  */
  2081.  
  2082.   if (!strcmp (name, "__.SYMDEF"))
  2083.     symdef_library (desc, entry, member_length);
  2084.   else
  2085.     linear_library (desc, entry);
  2086. }
  2087.  
  2088. /* Construct and return a file_entry for a library member.
  2089.    The library's file_entry is library_entry, and the library is open on DESC.
  2090.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  2091.    We store the length of the member into *LENGTH_LOC.  */
  2092.  
  2093. struct file_entry *
  2094. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  2095.      int desc;
  2096.      struct file_entry *library_entry;
  2097.      int subfile_offset;
  2098.      int *length_loc;
  2099. {
  2100.   int bytes_read;
  2101.   register int namelen;
  2102.   int member_length;
  2103.   register char *name;
  2104.   struct ar_hdr hdr1;
  2105.   register struct file_entry *subentry;
  2106.  
  2107.   lseek (desc, subfile_offset, 0);
  2108.  
  2109.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  2110.   if (!bytes_read)
  2111.     return 0;        /* end of archive */
  2112.  
  2113.   if (sizeof hdr1 != bytes_read)
  2114.     fatal_with_file ("malformed library archive ", library_entry);
  2115.  
  2116.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  2117.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  2118.  
  2119.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  2120.   bzero (subentry, sizeof (struct file_entry));
  2121.  
  2122.   for (namelen = 0;
  2123.        namelen < sizeof hdr1.ar_name
  2124.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  2125.        && hdr1.ar_name[namelen] != '/';
  2126.        namelen++);
  2127.  
  2128.   name = (char *) xmalloc (namelen+1);
  2129.   strncpy (name, hdr1.ar_name, namelen);
  2130.   name[namelen] = 0;
  2131.  
  2132.   subentry->filename = name;
  2133.   subentry->local_sym_name = name;
  2134.   subentry->symbols = 0;
  2135.   subentry->strings = 0;
  2136.   subentry->subfiles = 0;
  2137.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  2138.   subentry->superfile = library_entry;
  2139.   subentry->library_flag = 0;
  2140.   subentry->header_read_flag = 0;
  2141.   subentry->just_syms_flag = 0;
  2142.   subentry->chain = 0;
  2143.   subentry->total_size = member_length;
  2144.  
  2145.   (*length_loc) = member_length;
  2146.  
  2147.   return subentry;
  2148. }
  2149.  
  2150. int subfile_wanted_p ();
  2151.  
  2152. /* Search a library that has a __.SYMDEF member.
  2153.    DESC is a descriptor on which the library is open.
  2154.      The file pointer is assumed to point at the __.SYMDEF data.
  2155.    ENTRY is the library's file_entry.
  2156.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  2157.  
  2158. void
  2159. symdef_library (desc, entry, member_length)
  2160.      int desc;
  2161.      struct file_entry *entry;
  2162.      int member_length;
  2163. {
  2164.   int *symdef_data = (int *) xmalloc (member_length);
  2165.   register struct symdef *symdef_base;
  2166.   char *sym_name_base;
  2167.   int number_of_symdefs;
  2168.   int length_of_strings;
  2169.   int not_finished;
  2170.   int bytes_read;
  2171.   register int i;
  2172.   struct file_entry *prev = 0;
  2173.   int prev_offset = 0;
  2174.  
  2175.   bytes_read = read (desc, symdef_data, member_length);
  2176.   if (bytes_read != member_length)
  2177.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2178.  
  2179.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  2180.   if (number_of_symdefs < 0 ||
  2181.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  2182.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2183.  
  2184.   symdef_base = (struct symdef *) (symdef_data + 1);
  2185.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  2186.  
  2187.   if (length_of_strings < 0
  2188.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  2189.       + 2 * sizeof (int) != member_length)
  2190.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2191.  
  2192.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  2193.  
  2194.   /* Check all the string indexes for validity.  */
  2195.  
  2196.   for (i = 0; i < number_of_symdefs; i++)
  2197.     {
  2198.       register int index = symdef_base[i].symbol_name_string_index;
  2199.       if (index < 0 || index >= length_of_strings
  2200.       || (index && *(sym_name_base + index - 1)))
  2201.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2202.     }
  2203.  
  2204.   /* Search the symdef data for members to load.
  2205.      Do this until one whole pass finds nothing to load.  */
  2206.  
  2207.   not_finished = 1;
  2208.   while (not_finished)
  2209.     {
  2210.       not_finished = 0;
  2211.  
  2212.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  2213.      Load the library members that contain such symbols.  */
  2214.  
  2215.       for (i = 0;
  2216.        (i < number_of_symdefs
  2217.         && (undefined_global_sym_count || common_defined_global_count));
  2218.        i++)
  2219.     if (symdef_base[i].symbol_name_string_index >= 0)
  2220.       {
  2221.         register symbol *sp;
  2222.  
  2223.         sp = getsym_soft (sym_name_base
  2224.                   + symdef_base[i].symbol_name_string_index);
  2225.  
  2226.         /* If we find a symbol that appears to be needed, think carefully
  2227.            about the archive member that the symbol is in.  */
  2228.  
  2229.         if (sp && ((sp->referenced && !sp->defined)
  2230.                || (sp->defined && sp->max_common_size)))
  2231.           {
  2232.         int junk;
  2233.         register int j;
  2234.         register int offset = symdef_base[i].library_member_offset;
  2235.         struct file_entry *subentry;
  2236.  
  2237.         /* Don't think carefully about any archive member
  2238.            more than once in a given pass.  */
  2239.  
  2240.         if (prev_offset == offset)
  2241.           continue;
  2242.         prev_offset = offset;
  2243.  
  2244.         /* Read the symbol table of the archive member.  */
  2245.  
  2246.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  2247.         if (subentry == 0)
  2248.           fatal ("invalid offset for %s in symbol table of %s",
  2249.              sym_name_base
  2250.              + symdef_base[i].symbol_name_string_index,
  2251.              entry->filename);
  2252.         read_entry_symbols (desc, subentry);
  2253.         subentry->strings = (char *) malloc (subentry->string_size);
  2254.         read_entry_strings (desc, subentry);
  2255.  
  2256.         /* Now scan the symbol table and decide whether to load.  */
  2257.  
  2258.         if (!subfile_wanted_p (subentry))
  2259.           {
  2260.             free (subentry->symbols);
  2261.             free (subentry);
  2262.           }
  2263.         else
  2264.           {
  2265.             /* This member is needed; load it.
  2266.                Since we are loading something on this pass,
  2267.                we must make another pass through the symdef data.  */
  2268.  
  2269.             not_finished = 1;
  2270.  
  2271.             enter_file_symbols (subentry);
  2272.  
  2273.             if (prev)
  2274.               prev->chain = subentry;
  2275.             else entry->subfiles = subentry;
  2276.             prev = subentry;
  2277.  
  2278.             /* Clear out this member's symbols from the symdef data
  2279.                so that following passes won't waste time on them.  */
  2280.  
  2281.             for (j = 0; j < number_of_symdefs; j++)
  2282.               {
  2283.             if (symdef_base[j].library_member_offset == offset)
  2284.               symdef_base[j].symbol_name_string_index = -1;
  2285.               }
  2286.           }
  2287.  
  2288.         /* We'll read the strings again if we need them again.  */
  2289.         free (subentry->strings);
  2290.         subentry->strings = 0;
  2291.           }
  2292.       }
  2293.     }
  2294.  
  2295.   free (symdef_data);
  2296. }
  2297.  
  2298. /* Search a library that has no __.SYMDEF.
  2299.    ENTRY is the library's file_entry.
  2300.    DESC is the descriptor it is open on.  */
  2301.  
  2302. void
  2303. linear_library (desc, entry)
  2304.      int desc;
  2305.      struct file_entry *entry;
  2306. {
  2307.   register struct file_entry *prev = 0;
  2308.   register int this_subfile_offset = SARMAG;
  2309.  
  2310.   while (undefined_global_sym_count || common_defined_global_count)
  2311.     {
  2312.       int member_length;
  2313.       register struct file_entry *subentry;
  2314.  
  2315.       subentry = decode_library_subfile (desc, entry, this_subfile_offset,
  2316.                      &member_length);
  2317.  
  2318.       if (!subentry) return;
  2319.  
  2320.       read_entry_symbols (desc, subentry);
  2321.       subentry->strings = (char *) alloca (subentry->string_size);
  2322.       read_entry_strings (desc, subentry);
  2323.  
  2324.       if (!subfile_wanted_p (subentry))
  2325.     {
  2326.       free (subentry->symbols);
  2327.       free (subentry);
  2328.     }
  2329.       else
  2330.     {
  2331.       enter_file_symbols (subentry);
  2332.  
  2333.       if (prev)
  2334.         prev->chain = subentry;
  2335.       else entry->subfiles = subentry;
  2336.       prev = subentry;
  2337.       subentry->strings = 0; /* Since space will dissapear on return */
  2338.     }
  2339.  
  2340.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  2341.       if (this_subfile_offset & 1) this_subfile_offset++;
  2342.     }
  2343. }
  2344.  
  2345. /* ENTRY is an entry for a library member.
  2346.    Its symbols have been read into core, but not entered.
  2347.    Return nonzero if we ought to load this member.  */
  2348.  
  2349. int
  2350. subfile_wanted_p (entry)
  2351.      struct file_entry *entry;
  2352. {
  2353.   register struct nlist *p;
  2354.   register struct nlist *end
  2355.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2356. #ifdef DOLLAR_KLUDGE
  2357.   register int dollar_cond = 0;
  2358. #endif
  2359.  
  2360.   for (p = entry->symbols; p < end; p++)
  2361.     {
  2362.       register int type = p->n_type;
  2363.       register char *name = p->n_un.n_strx + entry->strings;
  2364.  
  2365.       /* If the symbol has an interesting definition, we could
  2366.      potentially want it.  */
  2367.       if (type & N_EXT
  2368.       && (type != (N_UNDF | N_EXT) || p->n_value
  2369.  
  2370. #ifdef DOLLAR_KLUDGE
  2371.            || name[1] == '$'
  2372. #endif
  2373.           )
  2374.       && !SET_ELEMENT_P (type)
  2375.       && !set_element_prefixed_p (name))
  2376.     {
  2377.       register symbol *sp = getsym_soft (name);
  2378.  
  2379. #ifdef DOLLAR_KLUDGE
  2380.       if (name[1] == '$')
  2381.         {
  2382.           sp = getsym_soft (&name[2]);
  2383.           dollar_cond = 1;
  2384.           if (!sp) continue;
  2385.           if (sp->referenced)
  2386.         {
  2387.           if (write_map)
  2388.             {
  2389.               print_file_name (entry, stdout);
  2390.               fprintf (stdout, " needed due to $-conditional %s\n", name);
  2391.             }
  2392.           return 1;
  2393.         }
  2394.           continue;
  2395.         }
  2396. #endif
  2397.  
  2398.       /* If this symbol has not been hashed, we can't be looking for it. */
  2399.  
  2400.       if (!sp) continue;
  2401.  
  2402.       if ((sp->referenced && !sp->defined)
  2403.           || (sp->defined && sp->max_common_size))
  2404.         {
  2405.           /* This is a symbol we are looking for.  It is either
  2406.              not yet defined or defined as a common.  */
  2407. #ifdef DOLLAR_KLUDGE
  2408.           if (dollar_cond) continue;
  2409. #endif
  2410.           if (type == (N_UNDF | N_EXT))
  2411.         {
  2412.           /* Symbol being defined as common.
  2413.              Remember this, but don't load subfile just for this.  */
  2414.  
  2415.           /* If it didn't used to be common, up the count of
  2416.              common symbols.  */
  2417.           if (!sp->max_common_size)
  2418.             common_defined_global_count++;
  2419.  
  2420.           if (sp->max_common_size < p->n_value)
  2421.             sp->max_common_size = p->n_value;
  2422.           if (!sp->defined)
  2423.             undefined_global_sym_count--;
  2424.           sp->defined = 1;
  2425.           continue;
  2426.         }
  2427.  
  2428.           if (write_map)
  2429.         {
  2430.           print_file_name (entry, stdout);
  2431.           fprintf (stdout, " needed due to %s\n", sp->name);
  2432.         }
  2433.           return 1;
  2434.         }
  2435.     }
  2436.     }
  2437.  
  2438.   return 0;
  2439. }
  2440.  
  2441. void consider_file_section_lengths (), relocate_file_addresses ();
  2442.  
  2443. /* Having entered all the global symbols and found the sizes of sections
  2444.    of all files to be linked, make all appropriate deductions from this data.
  2445.  
  2446.    We propagate global symbol values from definitions to references.
  2447.    We compute the layout of the output file and where each input file's
  2448.    contents fit into it.  */
  2449.  
  2450. void
  2451. digest_symbols ()
  2452. {
  2453.   register int i;
  2454.   int setv_fill_count;
  2455.  
  2456.   if (trace_files)
  2457.     fprintf (stderr, "Digesting symbol information:\n\n");
  2458.  
  2459.   /* Compute total size of sections */
  2460.  
  2461.   each_file (consider_file_section_lengths, 0);
  2462.  
  2463.   /* If necessary, pad text section to full page in the file.
  2464.      Include the padding in the text segment size.  */
  2465.  
  2466. #ifdef NMAGIC
  2467.   if (magic == ZMAGIC || magic == NMAGIC)
  2468. #else
  2469.   if (magic == ZMAGIC)
  2470. #endif
  2471.     {
  2472.       int text_end = text_size + N_TXTOFF (outheader);
  2473.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2474.       text_size += text_pad;
  2475.     }
  2476.  
  2477.   outheader.a_text = text_size;
  2478. #ifdef sequent
  2479.   outheader.a_text += N_ADDRADJ (outheader);
  2480. #endif
  2481.  
  2482.   /* Make the data segment address start in memory on a suitable boundary.  */
  2483.  
  2484.   if (! Tdata_flag_specified)
  2485.     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  2486.  
  2487.   /* Set up the set element vector */
  2488.  
  2489.   if (!relocatable_output)
  2490.     {
  2491.       /* The set sector size is the number of set elements + a word
  2492.          for each symbol for the length word at the beginning of the
  2493.      vector, plus a word for each symbol for a zero at the end of
  2494.      the vector (for incremental linking).  */
  2495.       set_sect_size
  2496.     = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  2497.       set_sect_start = data_start + data_size;
  2498.       data_size += set_sect_size;
  2499.       set_vectors = (unsigned long *) xmalloc (set_sect_size);
  2500.       setv_fill_count = 0;
  2501.     }
  2502.  
  2503.   /* Compute start addresses of each file's sections and symbols.  */
  2504.  
  2505.   each_full_file (relocate_file_addresses, 0);
  2506.  
  2507.   /* Now, for each symbol, verify that it is defined globally at most once.
  2508.      Put the global value into the symbol entry.
  2509.      Common symbols are allocated here, in the BSS section.
  2510.      Each defined symbol is given a '->defined' field
  2511.       which is the correct N_ code for its definition,
  2512.       except in the case of common symbols with -r.
  2513.      Then make all the references point at the symbol entry
  2514.      instead of being chained together. */
  2515.  
  2516.   defined_global_sym_count = 0;
  2517.  
  2518.   for (i = 0; i < TABSIZE; i++)
  2519.     {
  2520.       register symbol *sp;
  2521.       for (sp = symtab[i]; sp; sp = sp->link)
  2522.     {
  2523.       /* For each symbol */
  2524.       register struct nlist *p, *next;
  2525.       int defs = 0, com = sp->max_common_size;
  2526.       struct nlist *first_definition;
  2527.       for (p = sp->refs; p; p = next)
  2528.         {
  2529.           register int type = p->n_type;
  2530.  
  2531.           if (SET_ELEMENT_P (type))
  2532.         {
  2533.           if (relocatable_output)
  2534.             fatal ("internal: global ref to set element with -r");
  2535.           if (!defs++)
  2536.             {
  2537.               sp->value = set_sect_start
  2538.             + setv_fill_count++ * sizeof (unsigned long);
  2539.               sp->defined = N_SETV | N_EXT;
  2540.               first_definition = p;
  2541.             }
  2542.           else if ((sp->defined & ~N_EXT) != N_SETV)
  2543.             {
  2544.               sp->multiply_defined = 1;
  2545.               multiple_def_count++;
  2546.             }
  2547.           set_vectors[setv_fill_count++] = p->n_value;
  2548.         }
  2549.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  2550.         {
  2551.           /* non-common definition */
  2552.           if (defs++ && sp->value != p->n_value)
  2553.             {
  2554.               sp->multiply_defined = 1;
  2555.               multiple_def_count++;
  2556.             }
  2557.           sp->value = p->n_value;
  2558.           sp->defined = type;
  2559.           first_definition = p;
  2560.         }
  2561.           next = (struct nlist *) p->n_un.n_name;
  2562.           p->n_un.n_name = (char *) sp;
  2563.         }
  2564.       /* Allocate as common if defined as common and not defined for real */
  2565.       if (com && !defs)
  2566.         {
  2567.           if (!relocatable_output || force_common_definition)
  2568.         {
  2569.           int align = sizeof (int);
  2570.  
  2571.           /* Round up to nearest sizeof (int).  I don't know
  2572.              whether this is necessary or not (given that
  2573.              alignment is taken care of later), but it's
  2574.              traditional, so I'll leave it in.  Note that if
  2575.              this size alignment is ever removed, ALIGN above
  2576.              will have to be initialized to 1 instead of
  2577.              sizeof (int).  */
  2578.  
  2579.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  2580.  
  2581.           while (!(com & align))
  2582.             align <<= 1;
  2583.  
  2584.           align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
  2585.  
  2586.           bss_size = ((((bss_size + data_size + data_start)
  2587.                   + (align - 1)) & (- align))
  2588.                   - data_size - data_start);
  2589.  
  2590.           sp->value = data_start + data_size + bss_size;
  2591.           sp->defined = N_BSS | N_EXT;
  2592.           bss_size += com;
  2593.           if (write_map)
  2594.             printf ("Allocating common %s: %x at %x\n",
  2595.                 sp->name, com, sp->value);
  2596.         }
  2597.           else
  2598.         {
  2599.           sp->defined = 0;
  2600.           undefined_global_sym_count++;
  2601.         }
  2602.         }
  2603.       /* Set length word at front of vector and zero byte at end.
  2604.          Reverse the vector itself to put it in file order.  */
  2605.       if ((sp->defined & ~N_EXT) == N_SETV)
  2606.         {
  2607.           unsigned long length_word_index
  2608.         = (sp->value - set_sect_start) / sizeof (unsigned long);
  2609.           unsigned long i, tmp;
  2610.  
  2611.           set_vectors[length_word_index]
  2612.         = setv_fill_count - 1 - length_word_index;
  2613.  
  2614.           /* Reverse the vector.  */
  2615.           for (i = 1;
  2616.            i < (setv_fill_count - length_word_index - 1) / 2 + 1;
  2617.            i++)
  2618.         {
  2619.           tmp = set_vectors[length_word_index + i];
  2620.           set_vectors[length_word_index + i]
  2621.             = set_vectors[setv_fill_count - i];
  2622.           set_vectors[setv_fill_count - i] = tmp;
  2623.         }
  2624.  
  2625.           set_vectors[setv_fill_count++] = 0;
  2626.         }
  2627.       if (sp->defined)
  2628.         defined_global_sym_count++;
  2629.     }
  2630.     }
  2631.  
  2632.   if (end_symbol)        /* These are null if -r.  */
  2633.     {
  2634.       etext_symbol->value = text_size + text_start;
  2635.       edata_symbol->value = data_start + data_size;
  2636.       end_symbol->value = data_start + data_size + bss_size;
  2637.     }
  2638.  
  2639.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  2640.  
  2641.   if (specified_data_size && specified_data_size > data_size)
  2642.     data_pad = specified_data_size - data_size;
  2643.  
  2644.   if (magic == ZMAGIC)
  2645.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  2646.                - data_size;
  2647.  
  2648.   bss_size -= data_pad;
  2649.   if (bss_size < 0) bss_size = 0;
  2650.  
  2651.   data_size += data_pad;
  2652. }
  2653.  
  2654. /* Accumulate the section sizes of input file ENTRY
  2655.    into the section sizes of the output file.  */
  2656.  
  2657. void
  2658. consider_file_section_lengths (entry)
  2659.      register struct file_entry *entry;
  2660. {
  2661.   if (entry->just_syms_flag)
  2662.     return;
  2663.  
  2664.   entry->text_start_address = text_size;
  2665.   /* If there were any vectors, we need to chop them off */
  2666.   text_size += entry->header.a_text;
  2667.   entry->data_start_address = data_size;
  2668.   data_size += entry->header.a_data;
  2669.   entry->bss_start_address = bss_size;
  2670.   bss_size += entry->header.a_bss;
  2671.  
  2672.   text_reloc_size += entry->header.a_trsize;
  2673.   data_reloc_size += entry->header.a_drsize;
  2674. }
  2675.  
  2676. /* Determine where the sections of ENTRY go into the output file,
  2677.    whose total section sizes are already known.
  2678.    Also relocate the addresses of the file's local and debugger symbols.  */
  2679.  
  2680. void
  2681. relocate_file_addresses (entry)
  2682.      register struct file_entry *entry;
  2683. {
  2684.   entry->text_start_address += text_start;
  2685.   /* Note that `data_start' and `data_size' have not yet been
  2686.      adjusted for `data_pad'.  If they had been, we would get the wrong
  2687.      results here.  */
  2688.   entry->data_start_address += data_start;
  2689.   entry->bss_start_address += data_start + data_size;
  2690.  
  2691.   {
  2692.     register struct nlist *p;
  2693.     register struct nlist *end
  2694.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2695.  
  2696.     for (p = entry->symbols; p < end; p++)
  2697.       {
  2698.     /* If this belongs to a section, update it by the section's start address */
  2699.     register int type = p->n_type & N_TYPE;
  2700.  
  2701.     switch (type)
  2702.       {
  2703.       case N_TEXT:
  2704.       case N_SETT:
  2705.         p->n_value += entry->text_start_address;
  2706.         break;
  2707.       case N_DATA:
  2708.       case N_SETV:
  2709.       case N_SETD:
  2710.         /* A symbol whose value is in the data section
  2711.            is present in the input file as if the data section
  2712.            started at an address equal to the length of the file's text.  */
  2713.         p->n_value += entry->data_start_address - entry->header.a_text;
  2714.         break;
  2715.       case N_BSS:
  2716.       case N_SETB:
  2717.         /* likewise for symbols with value in BSS.  */
  2718.         p->n_value += entry->bss_start_address
  2719.           - entry->header.a_text - entry->header.a_data;
  2720.         break;
  2721.       }
  2722.       }
  2723.   }
  2724. }
  2725.  
  2726. void describe_file_sections (), list_file_locals ();
  2727.  
  2728. /* Print a complete or partial map of the output file.  */
  2729.  
  2730. void
  2731. print_symbols (outfile)
  2732.      FILE *outfile;
  2733. {
  2734.   register int i;
  2735.  
  2736.   fprintf (outfile, "\nFiles:\n\n");
  2737.  
  2738.   each_file (describe_file_sections, outfile);
  2739.  
  2740.   fprintf (outfile, "\nGlobal symbols:\n\n");
  2741.  
  2742.   for (i = 0; i < TABSIZE; i++)
  2743.     {
  2744.       register symbol *sp;
  2745.       for (sp = symtab[i]; sp; sp = sp->link)
  2746.     {
  2747.       if (sp->defined == 1)
  2748.         fprintf (outfile, "  %s: common, length 0x%x\n", sp->name, sp->max_common_size);
  2749.       if (sp->defined)
  2750.         fprintf (outfile, "  %s: 0x%x\n", sp->name, sp->value);
  2751.       else if (sp->referenced)
  2752.         fprintf (outfile, "  %s: undefined\n", sp->name);
  2753.     }
  2754.     }
  2755.  
  2756.   each_file (list_file_locals, outfile);
  2757. }
  2758.  
  2759. void
  2760. describe_file_sections (entry, outfile)
  2761.      struct file_entry *entry;
  2762.      FILE *outfile;
  2763. {
  2764.   fprintf (outfile, "  ");
  2765.   print_file_name (entry, outfile);
  2766.   if (entry->just_syms_flag)
  2767.     fprintf (outfile, " symbols only\n", 0);
  2768.   else
  2769.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  2770.          entry->text_start_address, entry->header.a_text,
  2771.          entry->data_start_address, entry->header.a_data,
  2772.          entry->bss_start_address, entry->header.a_bss);
  2773. }
  2774.  
  2775. void
  2776. list_file_locals (entry, outfile)
  2777.      struct file_entry *entry;
  2778.      FILE *outfile;
  2779. {
  2780.   register struct nlist
  2781.     *p,
  2782.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2783.  
  2784.   entry->strings = (char *) alloca (entry->string_size);
  2785.   read_entry_strings (file_open (entry), entry);
  2786.  
  2787.   fprintf (outfile, "\nLocal symbols of ");
  2788.   print_file_name (entry, outfile);
  2789.   fprintf (outfile, ":\n\n");
  2790.  
  2791.   for (p = entry->symbols; p < end; p++)
  2792.     /* If this is a definition,
  2793.        update it if necessary by this file's start address.  */
  2794.     if (!(p->n_type & (N_STAB | N_EXT)))
  2795.       fprintf (outfile, "  %s: 0x%x\n",
  2796.            entry->strings + p->n_un.n_strx, p->n_value);
  2797.  
  2798.   entry->strings = 0;        /* All done with them.  */
  2799. }
  2800.  
  2801.  
  2802. /* Static vars for do_warnings and subroutines of it */
  2803. int list_unresolved_refs;    /* List unresolved refs */
  2804. int list_warning_symbols;    /* List warning syms */
  2805. int list_multiple_defs;        /* List multiple definitions */
  2806.  
  2807. /*
  2808.  * Structure for communication between do_file_warnings and it's
  2809.  * helper routines.  Will in practice be an array of three of these:
  2810.  * 0) Current line, 1) Next line, 2) Source file info.
  2811.  */
  2812. struct line_debug_entry
  2813. {
  2814.   int line;
  2815.   char *filename;
  2816.   struct nlist *sym;
  2817. };
  2818.  
  2819. void qsort ();
  2820. /*
  2821.  * Helper routines for do_file_warnings.
  2822.  */
  2823.  
  2824. /* Return an integer less than, equal to, or greater than 0 as per the
  2825.    relation between the two relocation entries.  Used by qsort.  */
  2826.  
  2827. int
  2828. relocation_entries_relation (rel1, rel2)
  2829.      struct relocation_info *rel1, *rel2;
  2830. {
  2831.   return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  2832. }
  2833.  
  2834. /* Moves to the next debugging symbol in the file.  USE_DATA_SYMBOLS
  2835.    determines the type of the debugging symbol to look for (DSLINE or
  2836.    SLINE).  STATE_POINTER keeps track of the old and new locatiosn in
  2837.    the file.  It assumes that state_pointer[1] is valid; ie
  2838.    that it.sym points into some entry in the symbol table.  If
  2839.    state_pointer[1].sym == 0, this routine should not be called.  */
  2840.  
  2841. int
  2842. next_debug_entry (use_data_symbols, state_pointer)
  2843.      register int use_data_symbols;
  2844.      /* Next must be passed by reference! */
  2845.      struct line_debug_entry state_pointer[3];
  2846. {
  2847.   register struct line_debug_entry
  2848.     *current = state_pointer,
  2849.     *next = state_pointer + 1,
  2850.     /* Used to store source file */
  2851.     *source = state_pointer + 2;
  2852.   struct file_entry *entry = (struct file_entry *) source->sym;
  2853.  
  2854.   current->sym = next->sym;
  2855.   current->line = next->line;
  2856.   current->filename = next->filename;
  2857.  
  2858.   while (++(next->sym) < (entry->symbols
  2859.               + entry->header.a_syms/sizeof (struct nlist)))
  2860.     {
  2861.       /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  2862.        * may look negative...therefore, must mask to low bits
  2863.        */
  2864.       switch (next->sym->n_type & 0xff)
  2865.     {
  2866.     case N_SLINE:
  2867.       if (use_data_symbols) continue;
  2868.       next->line = next->sym->n_desc;
  2869.       return 1;
  2870.     case N_DSLINE:
  2871.       if (!use_data_symbols) continue;
  2872.       next->line = next->sym->n_desc;
  2873.       return 1;
  2874. #ifdef HAVE_SUN_STABS
  2875.     case N_EINCL:
  2876.       next->filename = source->filename;
  2877.       continue;
  2878. #endif
  2879.     case N_SO:
  2880.       source->filename = next->sym->n_un.n_strx + entry->strings;
  2881.       source->line++;
  2882. #ifdef HAVE_SUN_STABS
  2883.     case N_BINCL:
  2884. #endif
  2885.     case N_SOL:
  2886.       next->filename
  2887.         = next->sym->n_un.n_strx + entry->strings;
  2888.     default:
  2889.       continue;
  2890.     }
  2891.     }
  2892.   next->sym = (struct nlist *) 0;
  2893.   return 0;
  2894. }
  2895.  
  2896. /* Create a structure to save the state of a scan through the debug
  2897.    symbols.  USE_DATA_SYMBOLS is set if we should be scanning for
  2898.    DSLINE's instead of SLINE's.  entry is the file entry which points
  2899.    at the symbols to use.  */
  2900.  
  2901. struct line_debug_entry *
  2902. init_debug_scan (use_data_symbols, entry)
  2903.      int use_data_symbols;
  2904.      struct file_entry *entry;
  2905. {
  2906.   struct line_debug_entry
  2907.     *state_pointer
  2908.       = (struct line_debug_entry *)
  2909.     xmalloc (3 * sizeof (struct line_debug_entry));
  2910.   register struct line_debug_entry
  2911.     *current = state_pointer,
  2912.     *next = state_pointer + 1,
  2913.     *source = state_pointer + 2; /* Used to store source file */
  2914.  
  2915.   struct nlist *tmp;
  2916.  
  2917.   for (tmp = entry->symbols;
  2918.        tmp < (entry->symbols
  2919.           + entry->header.a_syms/sizeof (struct nlist));
  2920.        tmp++)
  2921.     if (tmp->n_type == (int) N_SO)
  2922.       break;
  2923.  
  2924.   if (tmp >= (entry->symbols
  2925.           + entry->header.a_syms/sizeof (struct nlist)))
  2926.     {
  2927.       /* I believe this translates to "We lose" */
  2928.       current->filename = next->filename = entry->filename;
  2929.       current->line = next->line = -1;
  2930.       current->sym = next->sym = (struct nlist *) 0;
  2931.       return state_pointer;
  2932.     }
  2933.  
  2934.   next->line = source->line = 0;
  2935.   next->filename = source->filename
  2936.     = (tmp->n_un.n_strx + entry->strings);
  2937.   source->sym = (struct nlist *) entry;
  2938.   next->sym = tmp;
  2939.  
  2940.   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  2941.  
  2942.   if (!next->sym)        /* No line numbers for this section; */
  2943.                 /* setup output results as appropriate */
  2944.     {
  2945.       if (source->line)
  2946.     {
  2947.       current->filename = source->filename = entry->filename;
  2948.       current->line = -1;    /* Don't print lineno */
  2949.     }
  2950.       else
  2951.     {
  2952.       current->filename = source->filename;
  2953.       current->line = 0;
  2954.     }
  2955.       return state_pointer;
  2956.     }
  2957.  
  2958.  
  2959.   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  2960.  
  2961.   return state_pointer;
  2962. }
  2963.  
  2964. /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER
  2965.    which describes the current location in the implied scan through
  2966.    the debug symbols within the file which ADDRESS is within, and
  2967.    returns the source line number which corresponds to ADDRESS.  */
  2968.  
  2969. int
  2970. address_to_line (address, state_pointer)
  2971.      unsigned long address;
  2972.      /* Next must be passed by reference! */
  2973.      struct line_debug_entry state_pointer[3];
  2974. {
  2975.   struct line_debug_entry
  2976.     *current = state_pointer,
  2977.     *next = state_pointer + 1;
  2978.   struct line_debug_entry *tmp_pointer;
  2979.  
  2980.   int use_data_symbols;
  2981.  
  2982.   if (next->sym)
  2983.     use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
  2984.   else
  2985.     return current->line;
  2986.  
  2987.   /* Go back to the beginning if we've already passed it.  */
  2988.   if (current->sym->n_value > address)
  2989.     {
  2990.       tmp_pointer = init_debug_scan (use_data_symbols,
  2991.                      (struct file_entry *)
  2992.                      ((state_pointer + 2)->sym));
  2993.       state_pointer[0] = tmp_pointer[0];
  2994.       state_pointer[1] = tmp_pointer[1];
  2995.       state_pointer[2] = tmp_pointer[2];
  2996.       free (tmp_pointer);
  2997.     }
  2998.  
  2999.   /* If we're still in a bad way, return -1, meaning invalid line.  */
  3000.   if (current->sym->n_value > address)
  3001.     return -1;
  3002.  
  3003.   while (next->sym
  3004.      && next->sym->n_value <= address
  3005.      && next_debug_entry (use_data_symbols, state_pointer))
  3006.     ;
  3007.   return current->line;
  3008. }
  3009.  
  3010.  
  3011. /* Macros for manipulating bitvectors.  */
  3012. #define    BIT_SET_P(bv, index)    ((bv)[(index) >> 3] & 1 << ((index) & 0x7))
  3013. #define    SET_BIT(bv, index)    ((bv)[(index) >> 3] |= 1 << ((index) & 0x7))
  3014.  
  3015. /* This routine will scan through the relocation data of file ENTRY,
  3016.    printing out references to undefined symbols and references to
  3017.    symbols defined in files with N_WARNING symbols.  If DATA_SEGMENT
  3018.    is non-zero, it will scan the data relocation segment (and use
  3019.    N_DSLINE symbols to track line number); otherwise it will scan the
  3020.    text relocation segment.  Warnings will be printed on the output
  3021.    stream OUTFILE.  Eventually, every nlist symbol mapped through will
  3022.    be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when
  3023.    we scan the nlists themselves.  */
  3024.  
  3025. do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector)
  3026.      struct file_entry *entry;
  3027.      int data_segment;
  3028.      FILE *outfile;
  3029.      unsigned char *nlist_bitvector;
  3030. {
  3031.   struct relocation_info
  3032.     *reloc_start = data_segment ? entry->datarel : entry->textrel,
  3033.     *reloc;
  3034.   int reloc_size
  3035.     = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  3036.        / sizeof (struct relocation_info));
  3037.   int start_of_segment
  3038.     = (data_segment ? entry->data_start_address : entry->text_start_address);
  3039.   struct nlist *start_of_syms = entry->symbols;
  3040.   struct line_debug_entry *state_pointer
  3041.     = init_debug_scan (data_segment != 0, entry);
  3042.   register struct line_debug_entry
  3043.     *current = state_pointer;
  3044.   /* Assigned to generally static values; should not be written into.  */
  3045.   char *errfmt;
  3046.   /* Assigned to alloca'd values cand copied into; should be freed
  3047.      when done.  */
  3048.   char *errmsg;
  3049.   int invalidate_line_number;
  3050.  
  3051.   /* We need to sort the relocation info here.  Sheesh, so much effort
  3052.      for one lousy error optimization. */
  3053.  
  3054.   qsort (reloc_start, reloc_size, sizeof (struct relocation_info),
  3055.      relocation_entries_relation);
  3056.  
  3057.   for (reloc = reloc_start;
  3058.        reloc < (reloc_start + reloc_size);
  3059.        reloc++)
  3060.     {
  3061.       register struct nlist *s;
  3062.       register symbol *g;
  3063.  
  3064.       /* If the relocation isn't resolved through a symbol, continue */
  3065.       if (!RELOC_EXTERN_P(reloc))
  3066.     continue;
  3067.  
  3068.       s = &(entry->symbols[RELOC_SYMBOL(reloc)]);
  3069.  
  3070.       /* Local symbols shouldn't ever be used by relocation info, so
  3071.      the next should be safe.
  3072.      This is, of course, wrong.  References to local BSS symbols can be
  3073.      the targets of relocation info, and they can (must) be
  3074.      resolved through symbols.  However, these must be defined properly,
  3075.      (the assembler would have caught it otherwise), so we can
  3076.      ignore these cases.  */
  3077.       if (!(s->n_type & N_EXT))
  3078.     continue;
  3079.  
  3080.       g = (symbol *) s->n_un.n_name;
  3081.       errmsg = 0;
  3082.  
  3083.       if (!g->defined && list_unresolved_refs) /* Reference */
  3084.     {
  3085.       /* Mark as being noted by relocation warning pass.  */
  3086.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3087.  
  3088.       if (g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3089.         continue;
  3090.  
  3091.       /* Undefined symbol which we should mention */
  3092.  
  3093.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3094.         {
  3095.           errfmt = "More undefined symbol %s refs follow";
  3096.           invalidate_line_number = 1;
  3097.         }
  3098.       else
  3099.         {
  3100.           errfmt = "Undefined symbol %s referenced from %s segment";
  3101.           invalidate_line_number = 0;
  3102.         }
  3103.     }
  3104.       else                         /* Defined */
  3105.     {
  3106.       /* Potential symbol warning here */
  3107.       if (!g->warning) continue;
  3108.  
  3109.       /* Mark as being noted by relocation warning pass.  */
  3110.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3111.  
  3112.       errfmt = 0;
  3113.       errmsg = g->warning;
  3114.       invalidate_line_number = 0;
  3115.     }
  3116.  
  3117.  
  3118.       /* If errfmt == 0, errmsg has already been defined.  */
  3119.       if (errfmt != 0)
  3120.     {
  3121.       char *nm;
  3122.  
  3123.       if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL)
  3124.         nm = g->name;
  3125.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1);
  3126.       sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text");
  3127.       if (nm != g->name)
  3128.         free (nm);
  3129.     }
  3130.  
  3131.       address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  3132.                state_pointer);
  3133.  
  3134.       if (current->line >=0)
  3135.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3136.          invalidate_line_number ? 0 : current->line, errmsg);
  3137.       else
  3138.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3139.  
  3140.       if (errfmt != 0)
  3141.     free (errmsg);
  3142.     }
  3143.  
  3144.   free (state_pointer);
  3145. }
  3146.  
  3147. /* Print on OUTFILE a list of all warnings generated by references
  3148.    and/or definitions in the file ENTRY.  List source file and line
  3149.    number if possible, just the .o file if not. */
  3150.  
  3151. void
  3152. do_file_warnings (entry, outfile)
  3153.      struct file_entry *entry;
  3154.      FILE *outfile;
  3155. {
  3156.   int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
  3157.   unsigned char *nlist_bitvector
  3158.     = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  3159.   struct line_debug_entry *text_scan, *data_scan;
  3160.   int i;
  3161.   char *errfmt, *file_name;
  3162.   int line_number;
  3163.   int dont_allow_symbol_name;
  3164.  
  3165.   bzero (nlist_bitvector, (number_of_syms >> 3) + 1);
  3166.  
  3167.   /* Read in the files strings if they aren't available */
  3168.   if (!entry->strings)
  3169.     {
  3170.       int desc;
  3171.  
  3172.       entry->strings = (char *) alloca (entry->string_size);
  3173.       desc = file_open (entry);
  3174.       read_entry_strings (desc, entry);
  3175.     }
  3176.  
  3177.   read_file_relocation (entry);
  3178.  
  3179.   /* Do text warnings based on a scan through the relocation info.  */
  3180.   do_relocation_warnings (entry, 0, outfile, nlist_bitvector);
  3181.  
  3182.   /* Do data warnings based on a scan through the relocation info.  */
  3183.   do_relocation_warnings (entry, 1, outfile, nlist_bitvector);
  3184.  
  3185.   /* Scan through all of the nlist entries in this file and pick up
  3186.      anything that the scan through the relocation stuff didn't.  */
  3187.  
  3188.   text_scan = init_debug_scan (0, entry);
  3189.   data_scan = init_debug_scan (1, entry);
  3190.  
  3191.   for (i = 0; i < number_of_syms; i++)
  3192.     {
  3193.       struct nlist *s;
  3194.       struct glosym *g;
  3195.  
  3196.       s = entry->symbols + i;
  3197.  
  3198.       if (!(s->n_type & N_EXT))
  3199.     continue;
  3200.  
  3201.       g = (symbol *) s->n_un.n_name;
  3202.       dont_allow_symbol_name = 0;
  3203.  
  3204.       if (list_multiple_defs && g->multiply_defined)
  3205.     {
  3206.       errfmt = "Definition of symbol %s (multiply defined)";
  3207.       switch (s->n_type)
  3208.         {
  3209.         case N_TEXT | N_EXT:
  3210.           line_number = address_to_line (s->n_value, text_scan);
  3211.           file_name = text_scan[0].filename;
  3212.           break;
  3213.         case N_DATA | N_EXT:
  3214.           line_number = address_to_line (s->n_value, data_scan);
  3215.           file_name = data_scan[0].filename;
  3216.           break;
  3217.         case N_SETA | N_EXT:
  3218.         case N_SETT | N_EXT:
  3219.         case N_SETD | N_EXT:
  3220.         case N_SETB | N_EXT:
  3221.           if (g->multiply_defined == 2)
  3222.         continue;
  3223.           errfmt = "First set element definition of symbol %s (multiply defined)";
  3224.           break;
  3225.         default:
  3226.           continue;        /* Don't print out multiple defs
  3227.                    at references.  */
  3228.         }
  3229.     }
  3230.       else if (BIT_SET_P (nlist_bitvector, i))
  3231.     continue;
  3232.       else if (list_unresolved_refs && !g->defined)
  3233.     {
  3234.       if (g->undef_refs >= MAX_UREFS_PRINTED)
  3235.         continue;
  3236.  
  3237.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3238.         errfmt = "More undefined \"%s\" refs follow";
  3239.       else
  3240.         errfmt = "Undefined symbol \"%s\" referenced";
  3241.       line_number = -1;
  3242.     }
  3243.       else if (g->warning)
  3244.     {
  3245.       /* There are two cases in which we don't want to
  3246.          do this.  The first is if this is a definition instead of
  3247.          a reference.  The second is if it's the reference used by
  3248.          the warning stabs itself.  */
  3249.       if (s->n_type != (N_EXT | N_UNDF)
  3250.           || (i && (s-1)->n_type == N_WARNING))
  3251.         continue;
  3252.  
  3253.       errfmt = g->warning;
  3254.       line_number = -1;
  3255.       dont_allow_symbol_name = 1;
  3256.     }
  3257.       else
  3258.     continue;
  3259.  
  3260.       if (line_number == -1)
  3261.     fprintf (outfile, "%s: ", entry->filename);
  3262.       else
  3263.     fprintf (outfile, "%s:%d: ", file_name, line_number);
  3264.  
  3265.       if (dont_allow_symbol_name)
  3266.     fprintf (outfile, "%s", errfmt);
  3267.       else
  3268.     {
  3269.       char *nm;
  3270.       if (demangler != NULL && (nm = (*demangler)(g->name)) != NULL)
  3271.         {
  3272.           fprintf (outfile, errfmt, nm);
  3273.           free (nm);
  3274.         }
  3275.       else
  3276.         fprintf (outfile, errfmt, g->name);
  3277.     }
  3278.  
  3279.       fputc ('\n', outfile);
  3280.     }
  3281.   free (text_scan);
  3282.   free (data_scan);
  3283.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  3284. }
  3285.  
  3286. do_warnings (outfile)
  3287.      FILE *outfile;
  3288. {
  3289.   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  3290.   list_warning_symbols = warning_count;
  3291.   list_multiple_defs = multiple_def_count != 0;
  3292.  
  3293.   if (!(list_unresolved_refs ||
  3294.     list_warning_symbols ||
  3295.     list_multiple_defs      ))
  3296.     /* No need to run this routine */
  3297.     return;
  3298.  
  3299.   each_file (do_file_warnings, outfile);
  3300.  
  3301.   if (list_unresolved_refs || list_multiple_defs)
  3302.     make_executable = 0;
  3303. }
  3304.  
  3305. /* Write the output file */
  3306.  
  3307. void
  3308. write_output ()
  3309. {
  3310.   struct stat statbuf;
  3311.   int filemode;
  3312.  
  3313.   (void) unlink (output_filename);
  3314.   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  3315.   if (outdesc < 0) perror_name (output_filename);
  3316.  
  3317.   if (fstat (outdesc, &statbuf) < 0)
  3318.     perror_name (output_filename);
  3319.  
  3320.   filemode = statbuf.st_mode;
  3321.  
  3322.   chmod (output_filename, filemode & ~0111);
  3323.  
  3324.   /* Output the a.out header.  */
  3325.   write_header ();
  3326.  
  3327.   /* Output the text and data segments, relocating as we go.  */
  3328.   write_text ();
  3329.   write_data ();
  3330.  
  3331.   /* Output the merged relocation info, if requested with `-r'.  */
  3332.   if (relocatable_output)
  3333.     write_rel ();
  3334.  
  3335.   /* Output the symbol table (both globals and locals).  */
  3336.   write_syms ();
  3337.  
  3338.   /* Copy any GDB symbol segments from input files.  */
  3339.   write_symsegs ();
  3340.  
  3341.   close (outdesc);
  3342.  
  3343.   if (chmod (output_filename, filemode | 0111) == -1)
  3344.     perror_name (output_filename);
  3345. }
  3346.  
  3347. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3348.  
  3349. void
  3350. write_header ()
  3351. {
  3352.   N_SET_MAGIC (outheader, magic);
  3353.   outheader.a_text = text_size;
  3354. #ifdef sequent
  3355.   outheader.a_text += N_ADDRADJ (outheader);
  3356.   if (entry_symbol == 0)
  3357.     entry_symbol = getsym("start");
  3358. #endif
  3359.   outheader.a_data = data_size;
  3360.   outheader.a_bss = bss_size;
  3361.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  3362.                : text_start + entry_offset);
  3363. #ifdef COFF_ENCAPSULATE
  3364.   if (need_coff_header)
  3365.     {
  3366.       /* We are encapsulating BSD format within COFF format.  */
  3367.       struct coffscn *tp, *dp, *bp;
  3368.  
  3369.       tp = &coffheader.scns[0];
  3370.       dp = &coffheader.scns[1];
  3371.       bp = &coffheader.scns[2];
  3372.  
  3373.       strcpy (tp->s_name, ".text");
  3374.       tp->s_paddr = text_start;
  3375.       tp->s_vaddr = text_start;
  3376.       tp->s_size = text_size;
  3377.       tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  3378.       tp->s_relptr = 0;
  3379.       tp->s_lnnoptr = 0;
  3380.       tp->s_nreloc = 0;
  3381.       tp->s_nlnno = 0;
  3382.       tp->s_flags = 0x20;
  3383.       strcpy (dp->s_name, ".data");
  3384.       dp->s_paddr = data_start;
  3385.       dp->s_vaddr = data_start;
  3386.       dp->s_size = data_size;
  3387.       dp->s_scnptr = tp->s_scnptr + tp->s_size;
  3388.       dp->s_relptr = 0;
  3389.       dp->s_lnnoptr = 0;
  3390.       dp->s_nreloc = 0;
  3391.       dp->s_nlnno = 0;
  3392.       dp->s_flags = 0x40;
  3393.       strcpy (bp->s_name, ".bss");
  3394.       bp->s_paddr = dp->s_vaddr + dp->s_size;
  3395.       bp->s_vaddr = bp->s_paddr;
  3396.       bp->s_size = bss_size;
  3397.       bp->s_scnptr = 0;
  3398.       bp->s_relptr = 0;
  3399.       bp->s_lnnoptr = 0;
  3400.       bp->s_nreloc = 0;
  3401.       bp->s_nlnno = 0;
  3402.       bp->s_flags = 0x80;
  3403.  
  3404.       coffheader.f_magic = COFF_MAGIC;
  3405.       coffheader.f_nscns = 3;
  3406.       /* store an unlikely time so programs can
  3407.        * tell that there is a bsd header
  3408.        */
  3409.       coffheader.f_timdat = 1;
  3410.       coffheader.f_symptr = 0;
  3411.       coffheader.f_nsyms = 0;
  3412.       coffheader.f_opthdr = 28;
  3413.       coffheader.f_flags = 0x103;
  3414.       /* aouthdr */
  3415.       coffheader.magic = ZMAGIC;
  3416.       coffheader.vstamp = 0;
  3417.       coffheader.tsize = tp->s_size;
  3418.       coffheader.dsize = dp->s_size;
  3419.       coffheader.bsize = bp->s_size;
  3420.       coffheader.entry = outheader.a_entry;
  3421.       coffheader.text_start = tp->s_vaddr;
  3422.       coffheader.data_start = dp->s_vaddr;
  3423.     }
  3424. #endif
  3425.  
  3426. #ifdef INITIALIZE_HEADER
  3427.   INITIALIZE_HEADER;
  3428. #endif
  3429.  
  3430.   if (strip_symbols == STRIP_ALL)
  3431.     nsyms = 0;
  3432.   else
  3433.     {
  3434.       nsyms = (defined_global_sym_count
  3435.            + undefined_global_sym_count);
  3436.       if (discard_locals == DISCARD_L)
  3437.     nsyms += non_L_local_sym_count;
  3438.       else if (discard_locals == DISCARD_NONE)
  3439.     nsyms += local_sym_count;
  3440.       /* One extra for following reference on indirects */
  3441.       if (relocatable_output)
  3442.     nsyms += set_symbol_count + global_indirect_count;
  3443.     }
  3444.  
  3445.   if (strip_symbols == STRIP_NONE)
  3446.     nsyms += debugger_sym_count;
  3447.  
  3448.   outheader.a_syms = nsyms * sizeof (struct nlist);
  3449.  
  3450.   if (relocatable_output)
  3451.     {
  3452.       outheader.a_trsize = text_reloc_size;
  3453.       outheader.a_drsize = data_reloc_size;
  3454.     }
  3455.   else
  3456.     {
  3457.       outheader.a_trsize = 0;
  3458.       outheader.a_drsize = 0;
  3459.     }
  3460.  
  3461. #ifdef COFF_ENCAPSULATE
  3462.   if (need_coff_header)
  3463.     mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  3464. #endif
  3465.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  3466.  
  3467.   /* Output whatever padding is required in the executable file
  3468.      between the header and the start of the text.  */
  3469.  
  3470. #ifndef COFF_ENCAPSULATE
  3471.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  3472. #endif
  3473. }
  3474.  
  3475. /* Relocate the text segment of each input file
  3476.    and write to the output file.  */
  3477.  
  3478. void
  3479. write_text ()
  3480. {
  3481.   if (trace_files)
  3482.     fprintf (stderr, "Copying and relocating text:\n\n");
  3483.  
  3484.   each_full_file (copy_text);
  3485.   file_close ();
  3486.  
  3487.   if (trace_files)
  3488.     fprintf (stderr, "\n");
  3489.  
  3490.   padfile (text_pad, outdesc);
  3491. }
  3492.  
  3493. int
  3494. text_offset (entry)
  3495.      struct file_entry *entry;
  3496. {
  3497.   return entry->starting_offset + N_TXTOFF (entry->header);
  3498. }
  3499.  
  3500. /* Read in all of the relocation information */
  3501.  
  3502. void
  3503. read_relocation ()
  3504. {
  3505.   each_full_file (read_file_relocation);
  3506. }
  3507.  
  3508. /* Read in the relocation sections of ENTRY if necessary */
  3509.  
  3510. void
  3511. read_file_relocation (entry)
  3512.      struct file_entry *entry;
  3513. {
  3514.   register struct relocation_info *reloc;
  3515.   int desc;
  3516.   int read_return;
  3517.  
  3518.   desc = -1;
  3519.   if (!entry->textrel)
  3520.     {
  3521.       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  3522.       desc = file_open (entry);
  3523.       lseek (desc,
  3524.          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  3525.          L_SET);
  3526.       if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
  3527.     {
  3528.       fprintf (stderr, "Return from read: %d\n", read_return);
  3529.       fatal_with_file ("premature eof in text relocation of ", entry);
  3530.     }
  3531.       entry->textrel = reloc;
  3532.     }
  3533.  
  3534.   if (!entry->datarel)
  3535.     {
  3536.       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  3537.       if (desc == -1) desc = file_open (entry);
  3538.       lseek (desc,
  3539.          text_offset (entry) + entry->header.a_text
  3540.          + entry->header.a_data + entry->header.a_trsize,
  3541.          L_SET);
  3542.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3543.     fatal_with_file ("premature eof in data relocation of ", entry);
  3544.       entry->datarel = reloc;
  3545.     }
  3546. }
  3547.  
  3548. /* Read the text segment contents of ENTRY, relocate them,
  3549.    and write the result to the output file.
  3550.    If `-r', save the text relocation for later reuse.  */
  3551.  
  3552. void
  3553. copy_text (entry)
  3554.      struct file_entry *entry;
  3555. {
  3556.   register char *bytes;
  3557.   register int desc;
  3558.   register struct relocation_info *reloc;
  3559.  
  3560.   if (trace_files)
  3561.     prline_file_name (entry, stderr);
  3562.  
  3563.   desc = file_open (entry);
  3564.  
  3565.   /* Allocate space for the file's text section */
  3566.  
  3567.   bytes = (char *) alloca (entry->header.a_text);
  3568.  
  3569.   /* Deal with relocation information however is appropriate */
  3570.  
  3571.   if (entry->textrel)  reloc = entry->textrel;
  3572.   else if (relocatable_output)
  3573.     {
  3574.       read_file_relocation (entry);
  3575.       reloc = entry->textrel;
  3576.     }
  3577.   else
  3578.     {
  3579.       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  3580.       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  3581.       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  3582.     fatal_with_file ("premature eof in text relocation of ", entry);
  3583.     }
  3584.  
  3585.   /* Read the text section into core.  */
  3586.  
  3587.   lseek (desc, text_offset (entry), 0);
  3588.   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  3589.     fatal_with_file ("premature eof in text section of ", entry);
  3590.  
  3591.  
  3592.   /* Relocate the text according to the text relocation.  */
  3593.  
  3594.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  3595.               reloc, entry->header.a_trsize, entry);
  3596.  
  3597.   /* Write the relocated text to the output file.  */
  3598.  
  3599.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  3600. }
  3601.  
  3602. /* Relocate the data segment of each input file
  3603.    and write to the output file.  */
  3604.  
  3605. void
  3606. write_data ()
  3607. {
  3608.   if (trace_files)
  3609.     fprintf (stderr, "Copying and relocating data:\n\n");
  3610.  
  3611.   each_full_file (copy_data);
  3612.   file_close ();
  3613.  
  3614.   /* Write out the set element vectors.  See digest symbols for
  3615.      description of length of the set vector section.  */
  3616.  
  3617.   if (set_vector_count)
  3618.     mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
  3619.          sizeof (unsigned long), outdesc);
  3620.  
  3621.   if (trace_files)
  3622.     fprintf (stderr, "\n");
  3623.  
  3624.   padfile (data_pad, outdesc);
  3625. }
  3626.  
  3627. /* Read the data segment contents of ENTRY, relocate them,
  3628.    and write the result to the output file.
  3629.    If `-r', save the data relocation for later reuse.
  3630.    See comments in `copy_text'.  */
  3631.  
  3632. void
  3633. copy_data (entry)
  3634.      struct file_entry *entry;
  3635. {
  3636.   register struct relocation_info *reloc;
  3637.   register char *bytes;
  3638.   register int desc;
  3639.  
  3640.   if (trace_files)
  3641.     prline_file_name (entry, stderr);
  3642.  
  3643.   desc = file_open (entry);
  3644.  
  3645.   bytes = (char *) alloca (entry->header.a_data);
  3646.  
  3647.   if (entry->datarel) reloc = entry->datarel;
  3648.   else if (relocatable_output)    /* Will need this again */
  3649.     {
  3650.       read_file_relocation (entry);
  3651.       reloc = entry->datarel;
  3652.     }
  3653.   else
  3654.     {
  3655.       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  3656.       lseek (desc, text_offset (entry) + entry->header.a_text
  3657.          + entry->header.a_data + entry->header.a_trsize,
  3658.          0);
  3659.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3660.     fatal_with_file ("premature eof in data relocation of ", entry);
  3661.     }
  3662.  
  3663.   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  3664.   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  3665.     fatal_with_file ("premature eof in data section of ", entry);
  3666.  
  3667.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  3668.               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  3669.  
  3670.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  3671. }
  3672.  
  3673. /* Relocate ENTRY's text or data section contents.
  3674.    DATA is the address of the contents, in core.
  3675.    DATA_SIZE is the length of the contents.
  3676.    PC_RELOCATION is the difference between the address of the contents
  3677.      in the output file and its address in the input file.
  3678.    RELOC_INFO is the address of the relocation info, in core.
  3679.    RELOC_SIZE is its length in bytes.  */
  3680. /* This version is about to be severly hacked by Randy.  Hope it
  3681.    works afterwards. */
  3682. void
  3683. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  3684.      char *data;
  3685.      struct relocation_info *reloc_info;
  3686.      struct file_entry *entry;
  3687.      int pc_relocation;
  3688.      int data_size;
  3689.      int reloc_size;
  3690. {
  3691.   register struct relocation_info *p = reloc_info;
  3692.   struct relocation_info *end
  3693.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  3694.   int text_relocation = entry->text_start_address;
  3695.   int data_relocation = entry->data_start_address - entry->header.a_text;
  3696.   int bss_relocation
  3697.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  3698.  
  3699.   for (; p < end; p++)
  3700.     {
  3701.       register int relocation = 0;
  3702.       register int addr = RELOC_ADDRESS(p);
  3703.       register unsigned int mask = 0;
  3704.  
  3705.       if (addr >= data_size)
  3706.     fatal_with_file ("relocation address out of range in ", entry);
  3707.  
  3708.       if (RELOC_EXTERN_P(p))
  3709.     {
  3710.       int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  3711.       symbol *sp = ((symbol *)
  3712.             (((struct nlist *)
  3713.               (((char *)entry->symbols) + symindex))
  3714.              ->n_un.n_name));
  3715.  
  3716. #ifdef N_INDR
  3717.       /* Resolve indirection */
  3718.       if ((sp->defined & ~N_EXT) == N_INDR)
  3719.         sp = (symbol *) sp->value;
  3720. #endif
  3721.  
  3722.       if (symindex >= entry->header.a_syms)
  3723.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3724.  
  3725.       /* If the symbol is undefined, leave it at zero.  */
  3726.       if (! sp->defined)
  3727.         relocation = 0;
  3728.       else
  3729.         relocation = sp->value;
  3730.     }
  3731.       else switch (RELOC_TYPE(p))
  3732.     {
  3733.     case N_TEXT:
  3734.     case N_TEXT | N_EXT:
  3735.       relocation = text_relocation;
  3736.       break;
  3737.  
  3738.     case N_DATA:
  3739.     case N_DATA | N_EXT:
  3740.       /* A word that points to beginning of the the data section
  3741.          initially contains not 0 but rather the "address" of that section
  3742.          in the input file, which is the length of the file's text.  */
  3743.       relocation = data_relocation;
  3744.       break;
  3745.  
  3746.     case N_BSS:
  3747.     case N_BSS | N_EXT:
  3748.       /* Similarly, an input word pointing to the beginning of the bss
  3749.          initially contains the length of text plus data of the file.  */
  3750.       relocation = bss_relocation;
  3751.       break;
  3752.  
  3753.     case N_ABS:
  3754.     case N_ABS | N_EXT:
  3755.       /* Don't know why this code would occur, but apparently it does.  */
  3756.       break;
  3757.  
  3758.     default:
  3759.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  3760.     }
  3761.  
  3762. #ifdef RELOC_ADD_EXTRA
  3763.       relocation += RELOC_ADD_EXTRA(p);
  3764.       if (relocatable_output)
  3765.     {
  3766.       /* Non-PC relative relocations with are absolute
  3767.          or which have become non-external now have fixed
  3768.          relocations.  Set the ADD_EXTRA of this relocation
  3769.          to be the relocation we have now determined.  */
  3770.       if (! RELOC_PCREL_P (p))
  3771.         {
  3772.           if ((int)p->r_type <= RELOC_32
  3773.           || RELOC_EXTERN_P (p) == 0)
  3774.         RELOC_ADD_EXTRA (p) = relocation;
  3775.         }
  3776.       /* External PC-relative relocations continue to move around;
  3777.          update their relocations by the amount they have moved
  3778.          so far.  */
  3779.       else if (RELOC_EXTERN_P (p))
  3780.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  3781.       continue;
  3782.     }
  3783. #endif
  3784.  
  3785.       if (RELOC_PCREL_P(p))
  3786.     relocation -= pc_relocation;
  3787.  
  3788.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  3789.  
  3790.       /* Unshifted mask for relocation */
  3791.       mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  3792.       mask |= mask - 1;
  3793.       relocation &= mask;
  3794.  
  3795.       /* Shift everything up to where it's going to be used */
  3796.       relocation <<= RELOC_TARGET_BITPOS(p);
  3797.       mask <<= RELOC_TARGET_BITPOS(p);
  3798.  
  3799.       switch (RELOC_TARGET_SIZE(p))
  3800.     {
  3801.     case 0:
  3802.       if (RELOC_MEMORY_SUB_P(p))
  3803.         relocation -= mask & *(char *) (data + addr);
  3804.       else if (RELOC_MEMORY_ADD_P(p))
  3805.         relocation += mask & *(char *) (data + addr);
  3806.       *(char *) (data + addr) &= ~mask;
  3807.       *(char *) (data + addr) |= relocation;
  3808.       break;
  3809.  
  3810.     case 1:
  3811.       if (RELOC_MEMORY_SUB_P(p))
  3812.         relocation -= mask & *(short *) (data + addr);
  3813.       else if (RELOC_MEMORY_ADD_P(p))
  3814.         relocation += mask & *(short *) (data + addr);
  3815.       *(short *) (data + addr) &= ~mask;
  3816.       *(short *) (data + addr) |= relocation;
  3817.       break;
  3818.  
  3819.     case 2:
  3820. #ifndef _CROSS_TARGET_ARCH
  3821.       if (RELOC_MEMORY_SUB_P(p))
  3822.         relocation -= mask & *(long *) (data + addr);
  3823.       else if (RELOC_MEMORY_ADD_P(p))
  3824.         relocation += mask & *(long *) (data + addr);
  3825.       *(long *) (data + addr) &= ~mask;
  3826.       *(long *) (data + addr) |= relocation;
  3827. #else
  3828.     /* Handle long word alignment requirements of SPARC architecture */
  3829.     /* WARNING:  This fix makes an assumption on byte ordering */
  3830.     /* Marc Ullman, Stanford University    Nov. 1 1989  */
  3831.       if (RELOC_MEMORY_SUB_P(p)) {
  3832.         relocation -= mask & 
  3833.           ((*(unsigned short *) (data + addr) << 16) |
  3834.         *(unsigned short *) (data + addr + 2));
  3835.       } else if (RELOC_MEMORY_ADD_P(p)) {
  3836.         relocation += mask &
  3837.           ((*(unsigned short *) (data + addr) << 16) |
  3838.         *(unsigned short *) (data + addr + 2));
  3839.       }
  3840.       *(unsigned short *) (data + addr)     &= (~mask >> 16);
  3841.       *(unsigned short *) (data + addr + 2) &= (~mask & 0xffff);
  3842.       *(unsigned short *) (data + addr)     |= (relocation >> 16);
  3843.       *(unsigned short *) (data + addr + 2) |= (relocation & 0xffff);
  3844. #endif
  3845.       break;
  3846.  
  3847.     default:
  3848.       fatal_with_file ("Unimplemented relocation field length in ", entry);
  3849.     }
  3850.     }
  3851. }
  3852.  
  3853. /* For relocatable_output only: write out the relocation,
  3854.    relocating the addresses-to-be-relocated.  */
  3855.  
  3856. void coptxtrel (), copdatrel ();
  3857.  
  3858. void
  3859. write_rel ()
  3860. {
  3861.   register int i;
  3862.   register int count = 0;
  3863.  
  3864.   if (trace_files)
  3865.     fprintf (stderr, "Writing text relocation:\n\n");
  3866.  
  3867.   /* Assign each global symbol a sequence number, giving the order
  3868.      in which `write_syms' will write it.
  3869.      This is so we can store the proper symbolnum fields
  3870.      in relocation entries we write.  */
  3871.  
  3872.   for (i = 0; i < TABSIZE; i++)
  3873.     {
  3874.       symbol *sp;
  3875.       for (sp = symtab[i]; sp; sp = sp->link)
  3876.     if (sp->referenced || sp->defined)
  3877.       {
  3878.         sp->def_count = count++;
  3879.         /* Leave room for the reference required by N_INDR, if
  3880.            necessary.  */
  3881.         if ((sp->defined & ~N_EXT) == N_INDR)
  3882.           count++;
  3883.       }
  3884.     }
  3885.   /* Correct, because if (reloatable_output), we will also be writing
  3886.      whatever indirect blocks we have.  */
  3887.   if (count != defined_global_sym_count
  3888.       + undefined_global_sym_count + global_indirect_count)
  3889.     fatal ("internal error");
  3890.  
  3891.   /* Write out the relocations of all files, remembered from copy_text.  */
  3892.  
  3893.   each_full_file (coptxtrel);
  3894.  
  3895.   if (trace_files)
  3896.     fprintf (stderr, "\nWriting data relocation:\n\n");
  3897.  
  3898.   each_full_file (copdatrel);
  3899.  
  3900.   if (trace_files)
  3901.     fprintf (stderr, "\n");
  3902. }
  3903.  
  3904. void
  3905. coptxtrel (entry)
  3906.      struct file_entry *entry;
  3907. {
  3908.   register struct relocation_info *p, *end;
  3909.   register int reloc = entry->text_start_address;
  3910.  
  3911.   p = entry->textrel;
  3912.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  3913.   while (p < end)
  3914.     {
  3915.       RELOC_ADDRESS(p) += reloc;
  3916.       if (RELOC_EXTERN_P(p))
  3917.     {
  3918.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  3919.       symbol *symptr = ((symbol *)
  3920.                 (((struct nlist *)
  3921.                   (((char *)entry->symbols) + symindex))
  3922.                  ->n_un.n_name));
  3923.  
  3924.       if (symindex >= entry->header.a_syms)
  3925.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3926.  
  3927. #ifdef N_INDR
  3928.       /* Resolve indirection.  */
  3929.       if ((symptr->defined & ~N_EXT) == N_INDR)
  3930.         symptr = (symbol *) symptr->value;
  3931. #endif
  3932.  
  3933.       /* If the symbol is now defined, change the external relocation
  3934.          to an internal one.  */
  3935.  
  3936.       if (symptr->defined)
  3937.         {
  3938.           RELOC_EXTERN_P(p) = 0;
  3939.           RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
  3940. #ifdef RELOC_ADD_EXTRA
  3941.           /* If we aren't going to be adding in the value in
  3942.              memory on the next pass of the loader, then we need
  3943.          to add it in from the relocation entry.  Otherwise
  3944.              the work we did in this pass is lost.  */
  3945.           if (!RELOC_MEMORY_ADD_P(p))
  3946.         RELOC_ADD_EXTRA (p) += symptr->value;
  3947. #endif
  3948.         }
  3949.       else
  3950.         /* Debugger symbols come first, so have to start this
  3951.            after them.  */
  3952.           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  3953.                  - defined_global_sym_count
  3954.                  - undefined_global_sym_count
  3955.                  - global_indirect_count);
  3956.     }
  3957.       p++;
  3958.     }
  3959.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  3960. }
  3961.  
  3962. void
  3963. copdatrel (entry)
  3964.      struct file_entry *entry;
  3965. {
  3966.   register struct relocation_info *p, *end;
  3967.   /* Relocate the address of the relocation.
  3968.      Old address is relative to start of the input file's data section.
  3969.      New address is relative to start of the output file's data section.  */
  3970.   register int reloc = entry->data_start_address - text_size;
  3971.  
  3972.   p = entry->datarel;
  3973.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  3974.   while (p < end)
  3975.     {
  3976.       RELOC_ADDRESS(p) += reloc;
  3977.       if (RELOC_EXTERN_P(p))
  3978.     {
  3979.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  3980.       symbol *symptr = ((symbol *)
  3981.                 (((struct nlist *)
  3982.                   (((char *)entry->symbols) + symindex))
  3983.                  ->n_un.n_name));
  3984.       int symtype;
  3985.  
  3986.       if (symindex >= entry->header.a_syms)
  3987.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3988.  
  3989. #ifdef N_INDR
  3990.       /* Resolve indirection.  */
  3991.       if ((symptr->defined & ~N_EXT) == N_INDR)
  3992.         symptr = (symbol *) symptr->value;
  3993. #endif
  3994.  
  3995.        symtype = symptr->defined & N_TYPE;
  3996.  
  3997.       if (force_common_definition
  3998.           || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  3999.         {
  4000.           RELOC_EXTERN_P(p) = 0;
  4001.           RELOC_SYMBOL(p) = symtype;
  4002.         }
  4003.       else
  4004.         /* Debugger symbols come first, so have to start this
  4005.            after them.  */
  4006.         RELOC_SYMBOL(p)
  4007.           = (((symbol *)
  4008.           (((struct nlist *)
  4009.             (((char *)entry->symbols) + symindex))
  4010.            ->n_un.n_name))
  4011.          ->def_count
  4012.          + nsyms - defined_global_sym_count
  4013.          - undefined_global_sym_count
  4014.          - global_indirect_count);
  4015.     }
  4016.       p++;
  4017.     }
  4018.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  4019. }
  4020.  
  4021. void write_file_syms ();
  4022. void write_string_table ();
  4023.  
  4024. /* Offsets and current lengths of symbol and string tables in output file. */
  4025.  
  4026. int symbol_table_offset;
  4027. int symbol_table_len;
  4028.  
  4029. /* Address in output file where string table starts.  */
  4030. int string_table_offset;
  4031.  
  4032. /* Offset within string table
  4033.    where the strings in `strtab_vector' should be written.  */
  4034. int string_table_len;
  4035.  
  4036. /* Total size of string table strings allocated so far,
  4037.    including strings in `strtab_vector'.  */
  4038. int strtab_size;
  4039.  
  4040. /* Vector whose elements are strings to be added to the string table.  */
  4041. char **strtab_vector;
  4042.  
  4043. /* Vector whose elements are the lengths of those strings.  */
  4044. int *strtab_lens;
  4045.  
  4046. /* Index in `strtab_vector' at which the next string will be stored.  */
  4047. int strtab_index;
  4048.  
  4049. /* Add the string NAME to the output file string table.
  4050.    Record it in `strtab_vector' to be output later.
  4051.    Return the index within the string table that this string will have.  */
  4052.  
  4053. int
  4054. assign_string_table_index (name)
  4055.      char *name;
  4056. {
  4057.   register int index = strtab_size;
  4058.   register int len = strlen (name) + 1;
  4059.  
  4060.   strtab_size += len;
  4061.   strtab_vector[strtab_index] = name;
  4062.   strtab_lens[strtab_index++] = len;
  4063.  
  4064.   return index;
  4065. }
  4066.  
  4067. FILE *outstream = (FILE *) 0;
  4068.  
  4069. /* Write the contents of `strtab_vector' into the string table.
  4070.    This is done once for each file's local&debugger symbols
  4071.    and once for the global symbols.  */
  4072.  
  4073. void
  4074. write_string_table ()
  4075. {
  4076.   register int i;
  4077.  
  4078.   lseek (outdesc, string_table_offset + string_table_len, 0);
  4079.  
  4080.   if (!outstream)
  4081.     outstream = fdopen (outdesc, "w");
  4082.  
  4083.   for (i = 0; i < strtab_index; i++)
  4084.     {
  4085.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  4086.       string_table_len += strtab_lens[i];
  4087.     }
  4088.  
  4089.   fflush (outstream);
  4090.  
  4091.   /* Report I/O error such as disk full.  */
  4092.   if (ferror (outstream))
  4093.     perror_name (output_filename);
  4094. }
  4095.  
  4096. /* Write the symbol table and string table of the output file.  */
  4097.  
  4098. void
  4099. write_syms ()
  4100. {
  4101.   /* Number of symbols written so far.  */
  4102.   int syms_written = 0;
  4103.   register int i;
  4104.   register symbol *sp;
  4105.  
  4106.   /* Buffer big enough for all the global symbols.  One
  4107.      extra struct for each indirect symbol to hold the extra reference
  4108.      following. */
  4109.   struct nlist *buf
  4110.     = (struct nlist *) alloca ((defined_global_sym_count
  4111.                 + undefined_global_sym_count
  4112.                 + global_indirect_count)
  4113.                    * sizeof (struct nlist));
  4114.   /* Pointer for storing into BUF.  */
  4115.   register struct nlist *bufp = buf;
  4116.  
  4117.   /* Size of string table includes the bytes that store the size.  */
  4118.   strtab_size = sizeof strtab_size;
  4119.  
  4120.   symbol_table_offset = N_SYMOFF (outheader);
  4121.   symbol_table_len = 0;
  4122.   string_table_offset = N_STROFF (outheader);
  4123.   string_table_len = strtab_size;
  4124.  
  4125.   if (strip_symbols == STRIP_ALL)
  4126.     return;
  4127.  
  4128.   /* Write the local symbols defined by the various files.  */
  4129.  
  4130.   each_file (write_file_syms, &syms_written);
  4131.   file_close ();
  4132.  
  4133.   /* Now write out the global symbols.  */
  4134.  
  4135.   /* Allocate two vectors that record the data to generate the string
  4136.      table from the global symbols written so far.  This must include
  4137.      extra space for the references following indirect outputs. */
  4138.  
  4139.   strtab_vector = (char **) alloca ((num_hash_tab_syms
  4140.                      + global_indirect_count) * sizeof (char *));
  4141.   strtab_lens = (int *) alloca ((num_hash_tab_syms
  4142.                  + global_indirect_count) * sizeof (int));
  4143.   strtab_index = 0;
  4144.  
  4145.   /* Scan the symbol hash table, bucket by bucket.  */
  4146.  
  4147.   for (i = 0; i < TABSIZE; i++)
  4148.     for (sp = symtab[i]; sp; sp = sp->link)
  4149.       {
  4150.     struct nlist nl;
  4151.  
  4152.     nl.n_other = 0;
  4153.     nl.n_desc = 0;
  4154.  
  4155.     /* Compute a `struct nlist' for the symbol.  */
  4156.  
  4157.     if (sp->defined || sp->referenced)
  4158.       {
  4159.         /* common condition needs to be before undefined condition */
  4160.         /* because unallocated commons are set undefined in */
  4161.         /* digest_symbols */
  4162.         if (sp->defined > 1) /* defined with known type */
  4163.           {
  4164.         /* If the target of an indirect symbol has been
  4165.            defined and we are outputting an executable,
  4166.            resolve the indirection; it's no longer needed */
  4167.         if (!relocatable_output
  4168.             && ((sp->defined & N_TYPE) == N_INDR)
  4169.             && (((symbol *) sp->value)->defined > 1))
  4170.           {
  4171.             symbol *newsp = (symbol *) sp->value;
  4172.             nl.n_type = newsp->defined;
  4173.             nl.n_value = newsp->value;
  4174.           }
  4175.         else
  4176.           {
  4177.             nl.n_type = sp->defined;
  4178.             if (sp->defined != (N_INDR | N_EXT))
  4179.               nl.n_value = sp->value;
  4180.             else
  4181.               nl.n_value = 0;
  4182.           }
  4183.           }
  4184.         else if (sp->max_common_size) /* defined as common but not allocated. */
  4185.           {
  4186.         /* happens only with -r and not -d */
  4187.         /* write out a common definition */
  4188.         nl.n_type = N_UNDF | N_EXT;
  4189.         nl.n_value = sp->max_common_size;
  4190.           }
  4191.         else if (!sp->defined)          /* undefined -- legit only if -r */
  4192.           {
  4193.         nl.n_type = N_UNDF | N_EXT;
  4194.         nl.n_value = 0;
  4195.           }
  4196.         else
  4197.           fatal ("internal error: %s defined in mysterious way", sp->name);
  4198.  
  4199.         /* Allocate string table space for the symbol name.  */
  4200.  
  4201.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  4202.  
  4203.         /* Output to the buffer and count it.  */
  4204.  
  4205.         *bufp++ = nl;
  4206.         syms_written++;
  4207.         if (nl.n_type == (N_INDR | N_EXT))
  4208.           {
  4209.         struct nlist xtra_ref;
  4210.         xtra_ref.n_type == N_EXT | N_UNDF;
  4211.         xtra_ref.n_un.n_strx
  4212.           = assign_string_table_index (((symbol *) sp->value)->name);
  4213.         xtra_ref.n_other = 0;
  4214.         xtra_ref.n_desc = 0;
  4215.         xtra_ref.n_value = 0;
  4216.         *bufp++ = xtra_ref;
  4217.         syms_written++;
  4218.           }
  4219.       }
  4220.       }
  4221.  
  4222.   /* Output the buffer full of `struct nlist's.  */
  4223.  
  4224.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4225.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4226.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4227.  
  4228.   if (syms_written != nsyms)
  4229.     fatal ("internal error: wrong number of symbols written into output file", 0);
  4230.  
  4231.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  4232.     fatal ("internal error: inconsistent symbol table length", 0);
  4233.  
  4234.   /* Now the total string table size is known, so write it.
  4235.      We are already positioned at the right place in the file.  */
  4236.  
  4237.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  4238.  
  4239.   /* Write the strings for the global symbols.  */
  4240.  
  4241.   write_string_table ();
  4242. }
  4243.  
  4244. /* Write the local and debugger symbols of file ENTRY.
  4245.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  4246.  
  4247. /* Note that we do not combine identical names of local symbols.
  4248.    dbx or gdb would be confused if we did that.  */
  4249.  
  4250. void
  4251. write_file_syms (entry, syms_written_addr)
  4252.      struct file_entry *entry;
  4253.      int *syms_written_addr;
  4254. {
  4255.   register struct nlist *p = entry->symbols;
  4256.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  4257.  
  4258.   /* Buffer to accumulate all the syms before writing them.
  4259.      It has one extra slot for the local symbol we generate here.  */
  4260.   struct nlist *buf
  4261.     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  4262.   register struct nlist *bufp = buf;
  4263.  
  4264.   /* Upper bound on number of syms to be written here.  */
  4265.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  4266.  
  4267.   /* Make tables that record, for each symbol, its name and its name's length.
  4268.      The elements are filled in by `assign_string_table_index'.  */
  4269.  
  4270.   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  4271.   strtab_lens = (int *) alloca (max_syms * sizeof (int));
  4272.   strtab_index = 0;
  4273.  
  4274.   /* Generate a local symbol for the start of this file's text.  */
  4275.  
  4276.   if (discard_locals != DISCARD_ALL)
  4277.     {
  4278.       struct nlist nl;
  4279.  
  4280.       nl.n_type = N_TEXT;
  4281.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  4282.       nl.n_value = entry->text_start_address;
  4283.       nl.n_desc = 0;
  4284.       nl.n_other = 0;
  4285.       *bufp++ = nl;
  4286.       (*syms_written_addr)++;
  4287.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  4288.     }
  4289.  
  4290.   /* Read the file's string table.  */
  4291.  
  4292.   entry->strings = (char *) alloca (entry->string_size);
  4293.   read_entry_strings (file_open (entry), entry);
  4294.  
  4295.   for (; p < end; p++)
  4296.     {
  4297.       register int type = p->n_type;
  4298.       register int write = 0;
  4299.  
  4300.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  4301.  
  4302.  
  4303.       if (SET_ELEMENT_P (type))    /* This occurs even if global.  These */
  4304.                 /* types of symbols are never written */
  4305.                 /* globally, though they are stored */
  4306.                 /* globally.  */
  4307.         write = relocatable_output;
  4308.       else if (!(type & (N_STAB | N_EXT)))
  4309.         /* ordinary local symbol */
  4310.     write = ((discard_locals != DISCARD_ALL)
  4311.          && !(discard_locals == DISCARD_L &&
  4312.               (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
  4313.          && type != N_WARNING);
  4314.       else if (!(type & N_EXT))
  4315.     /* debugger symbol */
  4316.         write = (strip_symbols == STRIP_NONE);
  4317.  
  4318.       if (write)
  4319.     {
  4320.       /* If this symbol has a name,
  4321.          allocate space for it in the output string table.  */
  4322.  
  4323.       if (p->n_un.n_strx)
  4324.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  4325.                             + entry->strings);
  4326.  
  4327.       /* Output this symbol to the buffer and count it.  */
  4328.  
  4329.       *bufp++ = *p;
  4330.       (*syms_written_addr)++;
  4331.     }
  4332.     }
  4333.  
  4334.   /* All the symbols are now in BUF; write them.  */
  4335.  
  4336.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4337.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4338.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4339.  
  4340.   /* Write the string-table data for the symbols just written,
  4341.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  4342.  
  4343.   write_string_table ();
  4344.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  4345. }
  4346.  
  4347. /* Copy any GDB symbol segments from the input files to the output file.
  4348.    The contents of the symbol segment is copied without change
  4349.    except that we store some information into the beginning of it.  */
  4350.  
  4351. void write_file_symseg ();
  4352.  
  4353. void
  4354. write_symsegs ()
  4355. {
  4356.   each_file (write_file_symseg, 0);
  4357. }
  4358.  
  4359. void
  4360. write_file_symseg (entry)
  4361.      struct file_entry *entry;
  4362. {
  4363.   char buffer[4096];
  4364.   struct symbol_root root;
  4365.   int indesc;
  4366.   int len;
  4367.  
  4368.   if (entry->symseg_offset == 0)
  4369.     return;
  4370.  
  4371.   /* This entry has a symbol segment.  Read the root of the segment.  */
  4372.  
  4373.   indesc = file_open (entry);
  4374.   lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  4375.   if (sizeof root != read (indesc, &root, sizeof root))
  4376.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  4377.  
  4378.   /* Store some relocation info into the root.  */
  4379.  
  4380.   root.ldsymoff = entry->local_syms_offset;
  4381.   root.textrel = entry->text_start_address;
  4382.   root.datarel = entry->data_start_address - entry->header.a_text;
  4383.   root.bssrel = entry->bss_start_address
  4384.     - entry->header.a_text - entry->header.a_data;
  4385.   root.databeg = entry->data_start_address - root.datarel;
  4386.   root.bssbeg = entry->bss_start_address - root.bssrel;
  4387.  
  4388.   /* Write the modified root into the output file.  */
  4389.  
  4390.   mywrite (&root, sizeof root, 1, outdesc);
  4391.  
  4392.   /* Copy the rest of the symbol segment unchanged.  */
  4393.  
  4394.   if (entry->superfile)
  4395.     {
  4396.       /* Library member: number of bytes to copy is determined
  4397.      from the member's total size.  */
  4398.  
  4399.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  4400.  
  4401.       while (total > 0)
  4402.     {
  4403.       len = read (indesc, buffer, min (sizeof buffer, total));
  4404.  
  4405.       if (len != min (sizeof buffer, total))
  4406.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  4407.       total -= len;
  4408.       mywrite (buffer, len, 1, outdesc);
  4409.     }
  4410.     }
  4411.   else
  4412.     {
  4413.       /* A separate file: copy until end of file.  */
  4414.  
  4415.       while (len = read (indesc, buffer, sizeof buffer))
  4416.     {
  4417.       mywrite (buffer, len, 1, outdesc);
  4418.       if (len < sizeof buffer)
  4419.         break;
  4420.     }
  4421.     }
  4422.  
  4423.   file_close ();
  4424. }
  4425.  
  4426. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  4427.  
  4428. void
  4429. symtab_init ()
  4430. {
  4431. #ifndef nounderscore
  4432.   edata_symbol = getsym ("_edata");
  4433.   etext_symbol = getsym ("_etext");
  4434.   end_symbol = getsym ("_end");
  4435. #else
  4436.   edata_symbol = getsym ("edata");
  4437.   etext_symbol = getsym ("etext");
  4438.   end_symbol = getsym ("end");
  4439. #endif
  4440.  
  4441. #ifdef sun
  4442.   {
  4443.     symbol *dynamic_symbol = getsym ("__DYNAMIC");
  4444.     dynamic_symbol->defined = N_ABS | N_EXT;
  4445.     dynamic_symbol->referenced = 1;
  4446.     dynamic_symbol->value = 0;
  4447.   }
  4448. #endif
  4449.  
  4450. #ifdef sequent
  4451.   {
  4452.     symbol *_387_flt_symbol = getsym ("_387_flt");
  4453.     _387_flt_symbol->defined = N_ABS | N_EXT;
  4454.     _387_flt_symbol->referenced = 1;
  4455.     _387_flt_symbol->value = 0;
  4456.   }
  4457. #endif
  4458.  
  4459.   edata_symbol->defined = N_DATA | N_EXT;
  4460.   etext_symbol->defined = N_TEXT | N_EXT;
  4461.   end_symbol->defined = N_BSS | N_EXT;
  4462.  
  4463.   edata_symbol->referenced = 1;
  4464.   etext_symbol->referenced = 1;
  4465.   end_symbol->referenced = 1;
  4466. }
  4467.  
  4468. /* Compute the hash code for symbol name KEY.  */
  4469.  
  4470. int
  4471. hash_string (key)
  4472.      char *key;
  4473. {
  4474.   register char *cp;
  4475.   register int k;
  4476.  
  4477.   cp = key;
  4478.   k = 0;
  4479.   while (*cp)
  4480.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  4481.  
  4482.   return k;
  4483. }
  4484.  
  4485. /* Get the symbol table entry for the global symbol named KEY.
  4486.    Create one if there is none.  */
  4487.  
  4488. symbol *
  4489. getsym (key)
  4490.      char *key;
  4491. {
  4492.   register int hashval;
  4493.   register symbol *bp;
  4494.  
  4495.   /* Determine the proper bucket.  */
  4496.  
  4497.   hashval = hash_string (key) % TABSIZE;
  4498.  
  4499.   /* Search the bucket.  */
  4500.  
  4501.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4502.     if (! strcmp (key, bp->name))
  4503.       return bp;
  4504.  
  4505.   /* Nothing was found; create a new symbol table entry.  */
  4506.  
  4507.   bp = (symbol *) xmalloc (sizeof (symbol));
  4508.   bp->refs = 0;
  4509.   bp->name = (char *) xmalloc (strlen (key) + 1);
  4510.   strcpy (bp->name, key);
  4511.   bp->defined = 0;
  4512.   bp->referenced = 0;
  4513.   bp->trace = 0;
  4514.   bp->value = 0;
  4515.   bp->max_common_size = 0;
  4516.   bp->warning = 0;
  4517.   bp->undef_refs = 0;
  4518.   bp->multiply_defined = 0;
  4519.  
  4520.   /* Add the entry to the bucket.  */
  4521.  
  4522.   bp->link = symtab[hashval];
  4523.   symtab[hashval] = bp;
  4524.  
  4525.   ++num_hash_tab_syms;
  4526.  
  4527.   return bp;
  4528. }
  4529.  
  4530. /* Like `getsym' but return 0 if the symbol is not already known.  */
  4531.  
  4532. symbol *
  4533. getsym_soft (key)
  4534.      char *key;
  4535. {
  4536.   register int hashval;
  4537.   register symbol *bp;
  4538.  
  4539.   /* Determine which bucket.  */
  4540.  
  4541.   hashval = hash_string (key) % TABSIZE;
  4542.  
  4543.   /* Search the bucket.  */
  4544.  
  4545.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4546.     if (! strcmp (key, bp->name))
  4547.       return bp;
  4548.  
  4549.   return 0;
  4550. }
  4551.  
  4552. /* Report a fatal error.
  4553.    STRING is a printf format string and ARG is one arg for it.  */
  4554.  
  4555. void
  4556. fatal (string, arg)
  4557.      char *string, *arg;
  4558. {
  4559.   fprintf (stderr, "ld: ");
  4560.   fprintf (stderr, string, arg);
  4561.   fprintf (stderr, "\n");
  4562.   exit (1);
  4563. }
  4564.  
  4565. /* Report a fatal error.  The error message is STRING
  4566.    followed by the filename of ENTRY.  */
  4567.  
  4568. void
  4569. fatal_with_file (string, entry)
  4570.      char *string;
  4571.      struct file_entry *entry;
  4572. {
  4573.   fprintf (stderr, "ld: ");
  4574.   fprintf (stderr, string);
  4575.   print_file_name (entry, stderr);
  4576.   fprintf (stderr, "\n");
  4577.   exit (1);
  4578. }
  4579.  
  4580. /* Report a fatal error using the message for the last failed system call,
  4581.    followed by the string NAME.  */
  4582.  
  4583. void
  4584. perror_name (name)
  4585.      char *name;
  4586. {
  4587.   extern int errno, sys_nerr;
  4588.   extern char *sys_errlist[];
  4589.   char *s;
  4590.  
  4591.   if (errno < sys_nerr)
  4592.     s = concat ("", sys_errlist[errno], " for %s");
  4593.   else
  4594.     s = "cannot open %s";
  4595.   fatal (s, name);
  4596. }
  4597.  
  4598. /* Report a fatal error using the message for the last failed system call,
  4599.    followed by the name of file ENTRY.  */
  4600.  
  4601. void
  4602. perror_file (entry)
  4603.      struct file_entry *entry;
  4604. {
  4605.   extern int errno, sys_nerr;
  4606.   extern char *sys_errlist[];
  4607.   char *s;
  4608.  
  4609.   if (errno < sys_nerr)
  4610.     s = concat ("", sys_errlist[errno], " for ");
  4611.   else
  4612.     s = "cannot open ";
  4613.   fatal_with_file (s, entry);
  4614. }
  4615.  
  4616. /* Report a nonfatal error.
  4617.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  4618.  
  4619. void
  4620. error (string, arg1, arg2, arg3)
  4621.      char *string, *arg1, *arg2, *arg3;
  4622. {
  4623.   fprintf (stderr, "%s: ", progname);
  4624.   fprintf (stderr, string, arg1, arg2, arg3);
  4625.   fprintf (stderr, "\n");
  4626. }
  4627.  
  4628.  
  4629. /* Output COUNT*ELTSIZE bytes of data at BUF
  4630.    to the descriptor DESC.  */
  4631.  
  4632. void
  4633. mywrite (buf, count, eltsize, desc)
  4634.      char *buf;
  4635.      int count;
  4636.      int eltsize;
  4637.      int desc;
  4638. {
  4639.   register int val;
  4640.   register int bytes = count * eltsize;
  4641.  
  4642.   while (bytes > 0)
  4643.     {
  4644.       val = write (desc, buf, bytes);
  4645.       if (val <= 0)
  4646.     perror_name (output_filename);
  4647.       buf += val;
  4648.       bytes -= val;
  4649.     }
  4650. }
  4651.  
  4652. /* Output PADDING zero-bytes to descriptor OUTDESC.
  4653.    PADDING may be negative; in that case, do nothing.  */
  4654.  
  4655. void
  4656. padfile (padding, outdesc)
  4657.      int padding;
  4658.      int outdesc;
  4659. {
  4660.   register char *buf;
  4661.   if (padding <= 0)
  4662.     return;
  4663.  
  4664.   buf = (char *) alloca (padding);
  4665.   bzero (buf, padding);
  4666.   mywrite (buf, padding, 1, outdesc);
  4667. }
  4668.  
  4669. /* Return a newly-allocated string
  4670.    whose contents concatenate the strings S1, S2, S3.  */
  4671.  
  4672. char *
  4673. concat (s1, s2, s3)
  4674.      char *s1, *s2, *s3;
  4675. {
  4676.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  4677.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  4678.  
  4679.   strcpy (result, s1);
  4680.   strcpy (result + len1, s2);
  4681.   strcpy (result + len1 + len2, s3);
  4682.   result[len1 + len2 + len3] = 0;
  4683.  
  4684.   return result;
  4685. }
  4686.  
  4687. /* Parse the string ARG using scanf format FORMAT, and return the result.
  4688.    If it does not parse, report fatal error
  4689.    generating the error message using format string ERROR and ARG as arg.  */
  4690.  
  4691. int
  4692. parse (arg, format, error)
  4693.      char *arg, *format;
  4694. {
  4695.   int x;
  4696.   if (1 != sscanf (arg, format, &x))
  4697.     fatal (error, arg);
  4698.   return x;
  4699. }
  4700.  
  4701. /* Like malloc but get fatal error if memory is exhausted.  */
  4702.  
  4703. int
  4704. xmalloc (size)
  4705.      int size;
  4706. {
  4707.   register int result = malloc (size);
  4708.   if (!result)
  4709.     fatal ("virtual memory exhausted", 0);
  4710.   return result;
  4711. }
  4712.  
  4713. /* Like realloc but get fatal error if memory is exhausted.  */
  4714.  
  4715. int
  4716. xrealloc (ptr, size)
  4717.      char *ptr;
  4718.      int size;
  4719. {
  4720.   register int result = realloc (ptr, size);
  4721.   if (!result)
  4722.     fatal ("virtual memory exhausted", 0);
  4723.   return result;
  4724. }
  4725.  
  4726. #ifdef USG
  4727.  
  4728. void
  4729. bzero (p, n)
  4730.      char *p;
  4731. {
  4732.   memset (p, 0, n);
  4733. }
  4734.  
  4735. void
  4736. bcopy (from, to, n)
  4737.      char *from, *to;
  4738. {
  4739.   memcpy (to, from, n);
  4740. }
  4741.  
  4742. getpagesize ()
  4743. {
  4744.   return (4096);
  4745. }
  4746.  
  4747. #endif
  4748.  
  4749. #if TARGET == SUN4
  4750.  
  4751. /* Don't use local pagesize to build for Sparc.  */
  4752.  
  4753. getpagesize ()
  4754. {
  4755.   return (8192);
  4756. }
  4757. #endif
  4758.